Hello,
I am currently trying to make a grid, say 10x10 that will represent a 2-D space. I want to make the numerical values 0-99 "boxes". These boxes will have a random number assigned to them. I tried to use some for loops and while loops. But all it did was assign one box 100 random numbers rather than box by box. I have butchered and gutted the code but included some of it. Any help will be greatly appreciated!
usingnamespace std;
int main(){
double r;
int box[100], count=0;
while (count <100){
for (int a=0; a<100; a++){
a= box[a];
r = ((double) rand()/(RAND_MAX+1));
cout << box[a] << "," << r << endl;
count = count+1;
}//closes for loop
}//closes while loop
}//closes main
May I ask why if you're making a 10x10 grid that represents 2D space, you don't just make a 10x10 array box[10][10], rather than using a single dimension array?
May I ask why if you're making a 10x10 grid that represents 2D space, you don't just make a 10x10 array box[10][10], rather than using a single dimension array?
Whether you declare the array as box[100] or box[10][10], the memory itself is contiguous and therefore very little difference. Declaring as a 2d array thus box[10][10] is probably easier to imagine a matrix with.
Made both changes and it worked. Originally I kept it as box[100] because eventually the program will have to check boxes around it. Figured it would be easier to check box [36] rather than [3][6]. Although it doesn't make too big of a difference to me. I ended up converting to 2-d arrays anyway. Thank you all for the help!