### Populate an array with random ints

This is where I am at so far:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include #include #define SIZE 3 using namespace std; /* Create a 3x3x3 array of random integers between 0 and 20. Then print to the screen the maximum value as well as its location in the array. */ int Random (int) { srand (time(NULL)); int i =rand () %20; return i; } int main (void) { int i = 0; //counter int j = 0; //counter int k = 0; //counter int Threes [SIZE][SIZE] ={ {Random (0),Random (1),Random (2),}, {Random (0),Random (1),Random (2),}, {Random (0),Random (1),Random (2),}, }; for (i = 0; i < SIZE ; i++) { for (j = 0; j < SIZE; j++) { //cout << j << "\t"; //for (k = 0; k < SIZE; k++) { cout << Threes [i][j] << '\t'; } } cout << endl; } return 0; } ``````

With output
 ``` Macon-Leightons-MacBook-3:me205 macon_l\$ ./a.out 3 3 3 3 3 3 3 3 3 Macon-Leightons-MacBook-3:me205 macon_l\$ ./a.out 17 17 17 17 17 17 17 17 17 Macon-Leightons-MacBook-3:me205 macon_l\$ ./a.out 11 11 11 11 11 11 11 11 11 Macon-Leightons-MacBook-3:me205 macon_l\$ ```

My strategy is to write a random number generator in it's own function, then create an array that calls the Random function each time the program is run, so that each array is random. I also reduced it down to a 2d array because I figured if I could get a 2d working, it would be very simple to add a 3rd dimension to it. Like god or something.

Anyways, my random number generator works fine, and I can get an array populated by random int's, but right now they are all the same. I understand that this is because each number in the array calls the random function at the same time, so Random only runs once. Without writing for loops inside the array, I'm not sure how to make each block of the array independent.

Am I on the right track, or do I need to go another direction?
Don't seed the random function everytime you use it.

So, add `srand(time(NULL));` to main where you only call it once.
Well that was easy. On to the maximums!
So here it is in 2d:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` `````` #include #include #include #define SIZE 3 using namespace std; /* Create a 3x3x3 array of random integers between 0 and 20. Then print to the screen the maximum value as well as its location in the array. */ int Random (int) { int i =rand () %20; return i; } int main (void) { srand (time(NULL)); int max = INT_MIN; int max2 = 0; int posi = 0; int posj = 0; int posk = 0; int i = 0; //counter int j = 0; //counter int k = 0; //counter int Threes [SIZE][SIZE]={ {Random (0),Random (1),Random (2),}, {Random (0),Random (1),Random (2),}, {Random (0),Random (1),Random (2),}, }; for (i = 0; i < SIZE ; i++) { for (j = 0; j < SIZE; j++) { //for (k = 0; k < SIZE; k++) { max2 = Threes [i][j]; if (max2 > max) { max = max2; } if (Threes [i][j] == max) { posi = i; posj = j; } cout << Threes [i][j]<< '\t'; } } cout << endl; } cout << "Max random value: " << max << endl; cout << "Location : Threes ["<< posi << "][" << posj << "]" << endl; return 0; }``````

Time to try 3d...
I just want to say thanks. I had the same problem with the random number.
So this seems to work:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` `````` #include #include #include #define SIZE 3 using namespace std; /* Create a 3x3x3 array of random integers between 0 and 20. Then print to the screen the maximum value as well as its location in the array. */ int Random (int) { int i =rand () %20; return i; } int main (void) { srand (time(NULL)); int max = INT_MIN; int max2 = 0; int posi = 0; int posj = 0; int posk = 0; int i = 0; //counter int j = 0; //counter int k = 0; //counter int Threes [SIZE][SIZE][SIZE]; for (i = 0; i < SIZE ; i++) { Threes[i][j][k] = Random (0); for (j = 0; j < SIZE; j++) { Threes[i][j][k] = Random (0); for (k = 0; k < SIZE; k++) { Threes[i][j][k] = Random (0); max2 = Threes [i][j][k]; if (max2 > max) { max = max2; } if (Threes [i][j][k] == max) { posi = i; posj = j; posk = k; } cout << Threes [i][j][k]<< '\t'; } } cout << endl; } cout << "Max random value: " << max << endl; cout << "Location : Threes ["<< posi << "][" << posj << "][" << posk << "]" << endl; return 0; }``````

Which outputs:
 ``` 16 4 2 15 18 8 15 18 4 10 0 15 7 10 4 1 9 17 15 10 15 16 0 18 0 10 13 Max random value: 18 Location : Threes [2][1][2] ```

But sometimes I get extra lines of output:
 ``` 8 13 15 3 7 7 8 1 14 9 7 13 11 9 8 1 15 16 0 13 2 0 14 4 4 1 17 16 4 6 18 0 16 11 11 19 11 13 16 6 12 12 5 6 0 Max random value: 19 Location : Threes [1][2][2] ```

WUT?!
Bump? Anyone have any ideas on this? I unloaded and quit everything and reloaded and it's still doing it. About 1 in 8 runs, the program outputs an extra line of code. WHY FOR YOU ADD LINES CODE?!
Seeing a lot of array questions tonight. This example might help some. The above code is pretty good, but I still haven't figured out the extra lines of output.

Again, seems to be about 1 in 8 times, which makes it most weird. Program runs fine, gives the correct output 7 out of 8 times, but then I get extra lines. Any ideas?
You're clobbering memory you don't own.

Remove lines 34 and 37.

j and k will be larger than 3 when their respective loops complete. Those lines were superfluous anyway. You kept reassigning to the same element in the array.

Prefer not to declare variables before their first use.

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````int Random () { return rand()%20 ; } int main (void) { srand (time(NULL)); int Threes[SIZE][SIZE][SIZE] ; int max = INT_MIN ; int posi = 0, posj = 0, posk=0 ; for (int i = 0; i < SIZE ; i++) { for (int j = 0; j < SIZE; j++) { for (int k = 0; k < SIZE; k++) { Threes[i][j][k] = Random(); int value = Threes [i][j][k]; if (value > max) { max = value; posi = i ; posj = j ; posk = k ; } cout << Threes [i][j][k]<< '\t'; } } cout << endl; } cout << "Max random value: " << max << endl; cout << "Location : Threes ["<< posi << "][" << posj << "][" << posk << "]" << endl; return 0; }``````

 You're clobbering memory you don't own.

Huh?

 Remove lines 34 and 37.

Ok.

 j and k will be larger than 3 when their respective loops complete. Those lines were superfluous anyway. You kept reassigning to the same element in the array.

How? Isn't that the point of

`j < SIZE`?

I mean it could be j <=SIZE, but that doesn't really matter here, because if j or k go over their size limits, then they are writing to memory outside of the array, right?

 Prefer not to declare variables before their first use.

Huh?

EDIT= I took out 34 and 37 and ran it about 50 times, seems like it solved the problem.

So the first loop with i actually populates the full array; I don't have to populate it with subsequent loops for each 'face' of the 3x3x3 cube?
Last edited on
 How? Isn't that the point of `j < SIZE?`

No. The point of `j<SIZE` is to keep from executing the for loop when J is greater than or equal to size. After the for loop, and before you reenter the for loop controlled by J or K, those variables are equal to SIZE, so using them to index the array means that you are accessing memory outside of the bounds of the array.

[edit: I incorrectly said in the earlier post that they would be greater than 3. What I meant was that they would be greater than the highest value of a valid index (which is 2.) Also corrected the for condition mishap]
Last edited on
I'm going to have to read that about 7 more times before I get it.
 The point of j

Do you mean to continue executing the for loop when J is less than size? You are turning my control structure world upside down.

 After the for loop, and before you reenter the for loop controlled by J or K, those variables are equal to SIZE

So the more robust fix is j<= SIZE?

 so using them to index the array means that you are accessing memory outside of the bounds of the array.

When j(or k) > SIZE we are outside of the array, but as long as it's less than it's just rewriting what 'i' already did, yes?

Last edited on
 Do you mean to continue executing the for loop when J is less than size? You are turning my control structure world upside down.

Yes. Sorry about that. It's corrected in the post.

 So the more robust fix is j<= SIZE?

No. The fix is not to use j (or k) outside of the for loop it controls. See the snippet of code I posted above.
I see that you moved everything into the k loop. It makes sense in my human brain that you would want everything manipulated at the deepest level, but in my computer brain, I don't understand how the array is populated before you get to the k loop? If you don't populate it in the i loop, then when you try to play with stuff in the k loop, it might be empty?
 I don't understand how the array is populated before you get to the k loop?

Before you get to the k loop you only have 2 indices to work with, so you cannot specify an element of the array - thus trying to populate the array before you get to the inner loop is silly. Every valid i/j/k combination is covered from within the inner loop.

 ``12345678910111213141516171819202122232425262728`` ``````#include int main() { const unsigned max = 3 ; std::cout << "[i] [j] [k]\n" ; for ( unsigned i=0; i
Topic archived. No new replies allowed.