Semi-simple C++ program help please

Hi, I'm working on a pretty simple program that outputs a 3d array of random numbers and then takes and outputs the 3 smallest and 3 largest numbers in the array. I have the array outputted, and I can get the smallest and largest numbers to print out. However, I can not get the 2nd and 3rd smallest/largest numbers. Here is my code: There might be a couple syntax errors in there by the way, since im typing over the whole thing rather than copy/paste (since its on command line right now)

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int fun[3][3][3]; // its a 3 by 3 by 3 int array
int high = 0;
int low = 0;

for (int i = 0; i < 3; i++)
{
cout << " " << endl;
for (int j = 0; j < 3; j++)
{
cout << " ";
for (int k = 0; k < 3; k++)
{
fun[i][j][k] = rand() % 9 + 1; // numbers in array have to be between 1 and 9
cout << fun[i][j][k];
if (fun [i][j][k] < low)
{
low = fun[i][j][k];
}
if (fun[i][j][k] > high)
{
high = fun[i][j][k];
}
}
}
}

cout << high;
cout << low;

return 0;
}

edit: Heres the output,

987 526 115 //it needed to be seperated into 3 blocks like that
456 877 547
213 268 361

9 // largest number
1 // smallest

Ok, so if I messed up somewhere in there, the actual program itself compiles and runs just so you know :) Any suggestions on how to get the next 2 smallest and largest numbers in line? Ive tried quite a few different ways and have come up empty handed. Im a begginer programmer so any info would be greatly appreciated. Thanks! PS. when I run this program the same random numbers come up every time. Is there somthing I would add in there to make them technically random every time as opposed to the same numbers each time? Thanks again.
[2203 byte] By [Jedd] at [2007-11-19 7:30:19]
# 1 Re: Semi-simple C++ program help please
A much simpler way would be to copy all your ints from the 3D array into a 1D vector (or array), sort it. Then the 3 last elements of the vector/array will be the 3 largest, and 3 first elements would be the 3 smallest. You can use the std::sort() function for sorting.

when I run this program the same random numbers come up every time. Is there somthing I would add in there to make them technically random every time as opposed to the same numbers each time? Thanks again.

2 ways:

1) You can call the srand() function which re-sets the seed value of the rabd() function to whatever seed value you provide. The return value of the time() function (which returns system time in seconds) is a good example of a number you could provide as a seed value. Just place this call at the beginning fo your program:

srand(time());

Note that you'll need to include the <ctime> (or <time.h>) library for the time() function.

2) You get random numbers between 1 and 9 in your program by using % on the return value of rand(). The resulting random number will come from the low-order bits of the return value of rand, which seem to be very un-random. Using a different method, you can get your random number from the high-order bits which are more random.

Instead of:

rand() % 9 + 1

use

(rand() * 9 / RAND_MAX) + 1

RAND_MAX is the largest possible value that rand() can return.
HighCommander4 at 2007-11-9 0:42:35 >