Making a grid

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using namespace 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 
line 12: You're assigning box[a] to your loop index (a). box[a] is uninitialized so you're going to be setting your loop index to garbage.

You probably meant:
1
2
 
  box[a] = a;


BTW, you really only need one loop. The while loop is unnecessary (at least for the code given).
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.
That was my point, it seems pointless not to declare as 2 dimensional when it uses no more memory and is more understandable in the context.
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!
Topic archived. No new replies allowed.