Hello 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). but when I run the program I get more characters than lines, which is a problem since there can be as many characters as lines :
Thank you for your response
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include "ballsortpuzzle.h" in this file I have declaration this function
#include <time.h>
void generator(const int rows, const int columns, char field[rows][columns]){
srand ( time(NULL) );
char characters[] = {'#', '@', '^', '*', '%', '+', '~', '?', '-', '/'};
int randomCol = rand() % columns;
int q = randomCol;
while( q == randomCol){
q = rand() % columns;
}
for( int i = 0 ; i < rows ; i++){
for( int k = 0; k < columns; k++){
int randomChar =rand() % 4;
int thisChar = characters[randomChar];
if(k == randomCol ){
field[i][k] = ' ';
}
else{
field[i][k] = thisChar;
}
}
}
for( int a = 0; a < rows; a++){
for(int l = 0 ; l < columns; l++) {
printf("%c " , field[a][l]);
}
printf("\n");
}
}
MAIN.c
#include <stdio.h>
#include <stdlib.h>
#include "ballsortpuzzle.h"
int main(){
char field[10][10];
generator(4,6,field);
}
ballsortpuzzle.h
#include <stdbool.h>
void generator(const int rows, const int columns, char field[rows][columns]);
"which is a problem since there can be as many characters as lines"
Do you mean"should"
instead of"can"
? – Andreas Wenzel