Help Please!!

I am trying to find out how to get the max and minimum values of 1-100 random numbers.

for(int i = 0; i<100; i++)
outfile << (rand()) << endl;

"I'm also outputting it to a file"
There are many ways of doing this, but here's one.

Get the first random number and assign it to variables holding maximum and minimum.

Loop a further 99 times.
- in each loop assign a random number to a variable; say x = rand();
- still within that loop, compare it with the current maximum and minimum, updating those if necessary.


dylpickle wrote:
"I'm also outputting it to a file"

What is "it"?

To write to the file:
- open an ofstream (say: outfile)
- output whatever "it" is when you have "it".
- (sometimes) close the stream. In most cases it will be closed automatically on leaving scope.
adding to above, a do-while lets you seed the min/max cleanly.
How I might solve the problem (brute force, stone knives and bear claws style):

1. instantiate a container, array or vector depending on how I feel when writing the code, loop 100 times and stuff a randomly chosen number into the container with each iteration.

2. instantiate two variables to hold min and max values. Loop 100 times and see if each previously stored random number is less than or greater than the currently held min/max numbers. If it is store it for future computation.

3. Open a file for writing, loop 100 times and write out each stored random number in the container to the opened file.

4. Close file.

5. Output needed information on data manipulation.

6. Success, program exits. The Homework Gnomes are cheering.

Notice I didn't mention exactly what type of loop used. That is up to the programmer (you) to decide.
Topic archived. No new replies allowed.