How to select positions of the matrix/ random draw?

Asked

Viewed 1,148 times

-3

Good morning! I need to do a random draw of the elements of the matrix to know if there is a boat (boat=1). As well as, at the end of 10 attempts need to print with (x) the successfully bombed positions, shots in the water (-), water (A), intáctos ships (N). It was complicated, but I have already managed to improve my program, but do 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);



}
  • 3

    As it is, I think the question is very broad because the correct answer is to write the entire program... It will be better to work it in modules (implement matrix; draw; position ships; shoot; etc) and come up with specific questions about each of them.

  • 1

    Hello, the question has no way to be canceled. If it did not have a positive punctuation response it could be erased, but it is not the case. Therefore, I would suggest you bring this question closer to your original version as possible, in a way that does not invalidate the existing answer, and by creating a new question that makes it clear the difference to this one.

1 answer

6

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

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

In the Nut way, you turn all this data into a single data string: A single memory pointer. Knowing the space that this entire string occupies, you will have a one-dimensional matrix and can achieve the desired value with just "one iteration".

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

  • Congratulations on the answer, explained well and did not give everything away..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.