How to select matrix positions / random draw? [closed]

-3

Good morning! I need to make a random draw from the array elements to find out if there is a boat (boat = 1). Just as at the end of 10 attempts I need to print with (x) the bombed positions successfully, shots on water (-), water (A), ships (N). It was complicated, but I have already managed to improve my program, but it still does not compile this part, please, if you can help me I am very grateful!

 #include <stdio.h>
 #include <stdlib.h>

 int main()
 {
char inimigo   [4][4] = {{0,1,1,0,}
                        {1,0,1,0},
                        {1,1,0,1},
                        {0,0,1,0}};


 char tabuleiro[4][4];
 int i,j;

for (i=0; i<4; i++){
 for(j=0; j<4; j++){
    tabuleiro[i][j] = '.';
    printf("%c", tabuleiro[i[j]);
}
printf("\n");
}
printf("Informe as coordenadas do tiro (linha/coluna):");
scanf("%d %d", &i &j);



}
    
asked by anonymous 04.10.2015 / 19:37

1 answer

6

This is simple. I can give you the chewed code, but you know. To choose the array at random you have two options: the right way and the nut way.

In the right way, the idea is to iterate through each member of the array, until there is no array / pointer. In your case, you only have two members so there will only be one iteration. After the first iteration (choose a random item), you select plus one , be this one of the "ships" (I suppose it is a boolean, 0 for absence and 1 for presence).

In the nutshell, you transform all of this data into a data-only string: a single memory pointer. Knowing the space that this entire string takes, you will have a one-dimensional array and you can achieve the desired value with just "one iteration."

Try to make the code for yourself; learn. If you can not think, have a helping hand .

    
04.10.2015 / 21:49