#include <iostream>
#include <cstdlib>
#include <ctime>
#define SIZE 3
usingnamespace 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;
}
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?
#include <iostream>
#include <cstdlib>
#include <ctime>
#define SIZE 3
usingnamespace 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;
}
#include <iostream>
#include <cstdlib>
#include <ctime>
#define SIZE 3
usingnamespace 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;
}
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?
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.
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?
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]
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.