Printing the largest 3 and smallest 3 values of 3D array

I just posted around an hour ago for help with the orientation of my array, but now I need some suggestions/help trying to figure out how to print the 3 smallest and 3 largest values of my program. Here is my code

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

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main() {
    
    srand(time(NULL));
    int fun[3][3][3];
    
    for ( int row=0; row<3; row++){
        for (int column=0; column<3; column++){
            for (int length=0; length<3; length++){
                int x = rand() % 10;
                fun[row][column][length] = x;
            }
        }
    }
    
    
    
    for (int column=0; column<3; column++){
        for ( int row=0; row<3; row++){
            for (int length=0; length<3; length++){
                cout << fun[row][column][length] << " ";
            } cout << "   ";
        } cout << endl;
    } cout << endl;
}


Once run, I would get this


8 3 7    7 6 7    0 0 2    
0 8 5    0 9 8    7 1 0    
0 2 5    5 8 1    1 5 7  


Now I need to print the smallest and largest 3 values which would be:

smallest: 0, 0, 0
largest: 8, 8, 9
Last edited on
Save the numbers in a std::vector<int> as the 3D array is being initialized and then you can sort the std::vector<int> to your heart's content!
1
2
3
4
5
6
7
8
9
10
11
 std::vector<int> v{};

    for ( int row=0; row<3; row++){
        for (int column=0; column<3; column++){
            for (int length=0; length<3; length++){
                int x = rand() % 10;
                fun[row][column][length] = x;
                v.push_back(x);
            }
        }
    }

BTW someone would mention sooner or later and so let it be me: rand() should be avoided:
http://www.cplusplus.com/forum/beginner/208037/
@gunnerfunner

How do you output the vector? also when I run this it outputs all the numbers instead of the largest 3 and smallest 3.
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
#include <iostream>
#include <random>
#include <vector>
#include <chrono>
#include <algorithm>

constexpr auto SIZE = 3;

int main()
{
    std::default_random_engine numGenerator(static_cast<unsigned int>(time(NULL)));
    std::uniform_int_distribution<int> range(0, 10);

    int fun[SIZE][SIZE][SIZE];
    std::vector<int> runs;
    runs.reserve (SIZE * SIZE * SIZE);

    for (size_t row = 0; row < SIZE; ++row)
    {
        for (size_t column = 0; column < SIZE; ++column)
        {
            for (size_t length = 0; length < SIZE; ++length)
            {
                int x = range(numGenerator);
                fun[row][column][length] = x;
                runs.push_back(x);
            }
        }
    }
    std::sort(runs.begin(), runs.end());
    for (const auto& elem : runs)std::cout << elem << " ";

    std::cout << "\nThree smallest numbers \n";
    for (size_t i = 0; i <= 2; ++i)
    {
        std::cout << runs[i] << " ";
    }
    std::cout << "\nThree largest numbers \n";
    for (size_t i = 3; i >= 1; --i)
    {
        std::cout << runs[runs.size() - i] << " ";
    }
}
Topic archived. No new replies allowed.