Program I am trying to complete initializes a three dimensional array with random values between 0-9, prints the array, and searches the array for the 3 smallest and 3 largest integers, and prints those values. Does not seem to find the correct values, so I am sure there is a flaw in my logic somewhere, but I cannot for the life of me figure out what it is.
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main()
{
srand(42); //Initializing random value seed
int lowest[3]; //Variable declaration
int highest[3];
int fun[3][3][3];
int count = 0;
int count_highest = 0;
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
for (int d = 0; d < 3; d++)
{
fun[r][c][d] = rand() % 10; //Initializing the random values inside the elements
}
}
}
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
for (int d = 0; d < 3; d++)
{
cout << fun[r][c][d] << " "; //Printing array
}
}
cout << endl;
}
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
for (int d = 0; d < 3; d++)
{
lowest[d] = fun[0][0][0]; //Setting all lowest values to element 0
highest[d] = fun[0][0][0]; //Setting all highest values to element 0
}
}
}
for (int i = 0; i < 3; i++) //Check I put in to evaluate if the above for loop was initializing the lowest and highest values correctly
{
cout << "Highest at " << i << " is equal to " << highest[i] << endl;
cout << "Lowest at " << i << " is equal to " << lowest[i] << endl;
}
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
for (int d = 0; d < 3; d++)
{
if (fun[r][c][d] <= lowest[count])
{
lowest[count] = fun[r][c][d];
count++;
}
}
}
}
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
for (int d = 0; d < 3; d++)
{
if (fun[r][c][d] >= highest[count])
{
highest[count_highest] = fun[r][c][d]; //Highest at 0, 1, or 2 is equal to fun if the value is greater than or equal to highest
count_highest++;
}
}
}
}
for (int i = 0; i < 3; i++)
{
cout << "Highest value at " << i + 1 << " is: " << highest[i] << endl;
}
for (int i = 0; i < 3; i++)
{
cout << "Lowest value at " << i + 1 << " is: " << lowest[i] << endl;
}
return 0;
}
Hi const
Well I can give you one clue as to what might be going wrong.
Look at the variable you have called count. Initialized to 0 at the top of your main function. You're using it as an index into the highest and lowest arrays, both of which are of size 3. So valid indices would be 0 to 2.
But here's what you get when you print out the value of count just before it's used in those 'if' statements:
So it's used 54 times but only the first 3 times does it hold a valid index. The rest of the time it's just pumping garbage into your program - garbage in, garbage out!
(There's a similar problem with the count_highest variable).
Use std::vector::assign to flatten the 3D array into a std::vector – then you can std::sort the vector for the 3 largest and 3 smallest elements
There are also some posts that using a std::priority queue would be more efficient (O(nlogk) vs O(nlogn)) to find the k smallest/largest elements in a vector vis-a-vis std::sort but in your case you need to find both n largest and n smallest, so perhaps one std::sort outright? Here's the link: http://stackoverflow.com/questions/14902876/indices-of-the-k-largest-elements-in-an-unsorted-length-n-array