Hi everybody, good afternoon.
- I made a code to the problem below (in blue), but it seems that my logic is in some hole. Already I revised, and do not understand why I still have repeated numbers. Can anyone give me strength? Thank you!!
- The logic that I thought code is as follows: I fill in a field of the matrix, with "i" and "j" and compare it to previous ones through another FOR with "k" and "z". If this field that just fill is equal to a previous field, I go back a position (j = j-1) and start the FOR again. To do so we made use FOR`s 4 (one inside the other, to enter the matrix and make the comparison), four variables (i, j, k, z) and an "aux" that serves as a precondition for my stop.
- Important: I made the code with a 4x4 matrix to test it. Then I will replicate for a 99x99 matrix.
- The problem: Make a program to automatically generate numbers between 0 and 99 for a bingo card. Knowing that each card should contain 5 lines and 5 numbers, manages this data so as not to have repeated numbers in the chart.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main () {
int M[4][4], i,j,k,z, aux = 0;
srand(time(NULL));
for(i=0;i<4;i++){
for(j=0;j<4;j++){
aux = 0;
M[i][j] = i;
for(k = i; k >= 0; k--){
if(aux == 1){
break;
}
for(z = j; z >= 0; z--){
if((M[k][z] == M[i][j]) && ((k != i) && (z != j))){
aux = 1;
j = j - 1;
break;
}
}
}
}
}
printf("\n\n\n\n");
for(i=0;i<4;i++){
for(j=0;j<4;j++){
printf("%d\t", M[i][j]);
}
printf("\n");
}
return 0;
}