So, here is the thing. I am dissecting some code to try and better understand how to finish my project (I'm a student. Made it to the final project without getting too lost!) and I am a little stuck. This part of the code randomly fills a 32x32 2D array with 0's and 1's. The 1's are what I need to focus on. Is there a way to show a blank space instead of a 0 so you just see the 1's?

Here is the array parts of the code! If you need to see more of it I can supply it! Didn't want to violate the "don't drown us in code" part right off the bat!

``````for(int i=0;i<steps;i++){

wait(1);
printArray();
checkNeighbor();
swapGrids();
//printf("\a");
}

scanf("%d");
return 0;
}

int fillArray(){ // fills the array with random cells. everything is a zero or a one.
int random=0;
srand(time(NULL));

for(int i=1;i<=size-1;i++){
for(int j=1;j<=size-1;j++){
random=rand()%2+0;
grid[i][j]=random;

}
}
return 0;
}

int printArray(){
system("cls");
//print all the stuff that's been pushed into our array
for(int i=0;i<=size;i++){
printf("\n");
for(int j=0;j<=size;j++){
printf("%d ",grid[i][j]);

}
}

return 0;
}``````
3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Arbus

yes using a condition loop :

``````for(int i = 0; i < size; ++i){
for(int j = 0; j < size; ++j){
if(grid[i][j] == 0) cout << " ";
else cout << grid[i][j];
}
cout << endl;
}``````

in your loop you go from i = 0 to i <=size; Make sure you array is of size, size+1 for that loop to be correct, or else you a bug

Hello thecodepixie,
In the for loop in line 37 use some if-else statements. For example

``````if(grid[i][j]==0)
{printf(" ");
}
else
{printf("%d ",grid[i][j]);
}``````

Edited by Arbus: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.