I'm working on a program in C that fills a 2d array with random characters. This field can only contain as many types of characters as the column array has, but one character can be in the array only as many times as the rows array has.
Code:
int main(){
srand ( time(NULL) );
char numbers[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'};
int rows = 4;
int columns = 6;
char field[rows][columns];
for(int j = 0; j < rows; j++){
for(int i = 0; i < columns; i++){
int random_znak = rand() % columns;
char random = numbers[random_znak];
field[j][i] = random;
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("%c ", field[i][j]);
}
printf("\n");
}
}
Image from output:

I have array with 4 rows and 6 columns. The problem is that I have four rows but several times letter B. I have for example five times letter B and only three times letter E. But the letter B and the letter E should have been in the array four times.
I hope you understand my problem.
Aucun commentaire:
Enregistrer un commentaire