I'm trying to make a program in C that will generate an 2d array with random characters, but two columns must still be empty (filled with space).
Example: (red columns are empty)
[1]: https://i.stack.imgur.com/2aIJW.png
But the problem is that one character can be as many times as there are rows. There are four rows in the picture, so one character can be a maximum of four times. But I have for example 6 times # and only 3 times * .
Code :
void generator(const int rows, const int columns, char field[rows][columns]){
srand ( time(NULL) );
char characters[] = {'#', '@', '^', '*', '%', '+', '~', '?', '-', '/'};
int r = rand() % columns;
int q = r;
while(q == r){
q = rand() % columns;
}
for(int j = 0; j < rows; j++){
for(int i = 0; i < columns; i++){
int random_znak = rand() % 4;
int random = characters[random_znak];
if( i == r || i == q){
field[j][i] = ' ';
}else{
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");
}
}
int main(){
char field[10][10];
generator(4 , 6, field);
}
Aucun commentaire:
Enregistrer un commentaire