Searching multi dimensional array for smallest and greatest values

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 #include <iostream>
#include <cstdlib>
using namespace 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:
count=0
count=1
count=2
count=3
count=3
count=3
count=3
count=3
count=4
count=5
count=6
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7
count=7


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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <random>
#include <vector>
#include <chrono>
#include <algorithm>

constexpr auto SIZE = 3;
constexpr auto low_bound = 0;
constexpr auto up_bound = 9;

int main()
{
    auto seed = std::chrono::system_clock::now().time_since_epoch().count();//seed
    std::default_random_engine dre(seed);//engine
    std::uniform_int_distribution<int> di(low_bound,up_bound);//distribution

    int myArray [SIZE][SIZE][SIZE];
    for (size_t i = 0; i < SIZE; ++i)
    {
        for (size_t j = 0; j < SIZE; ++j)
        {
            std::generate(myArray[i][j], myArray[i][j] + SIZE, [&]{ return di(dre);} );
        }
    }
    //http://en.cppreference.com/w/cpp/algorithm/generate
    for (size_t i = 0; i < SIZE; ++i)
    {
        for (size_t j = 0; j < SIZE; ++j)
        {
            for (size_t k = 0; k < SIZE; ++k)
            {
                std::cout << myArray[i][j][k] << " ";
            }
            std::cout << "\n";
            }
        std::cout << "\n";
    }
    std::vector<int> vec{};
    for (size_t i = 0; i < SIZE; ++i)
    {
        for (size_t j = 0; j < SIZE; ++j)
        {
            std::vector<int> tempVec{};
            tempVec.assign(myArray[i][j], myArray[i][j]+ SIZE);
            vec.insert(vec.end(), tempVec.begin(), tempVec.end());
        }
    }
    std::cout << "Flattened vector: \n";
    for (const auto & elem : vec) std::cout << elem << " "; std::cout << "\n";
    std::sort(vec.begin(), vec.end());
    std::cout << "Smallest three numbers: \n";
    for (size_t i = 0; i < 3; ++i) std::cout << *(vec.begin() + i) << " "; std::cout << "\n";
    std::cout << "Largest three numbers: \n";
    for (size_t i = 0; i < 3; ++i) std::cout << *(vec.rbegin() + i) << " "; std::cout << "\n";
}
Topic archived. No new replies allowed.