#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
srand (time(0));
int count = 0;
int random_num = (rand () % 100) + 1;
int high_num = random_num;
int low_num = random_num;
float total = 0.0;
while (count < 100000){
count++;
random_num = (rand () % 100) + 1;
if (high_num < random_num)
high_num = random_num;
if (low_num > random_num)
low_num = random_num;
total = total + random_num;
}
float average = total / 100000;
cout << "The high number is: " << high_num << endl;
cout << "The low number is: " << low_num << endl;
cout << "The average is: " << average << endl;
system ("Pause");
return 0;
}
The program compiles and runs. It generates random numbers, displays the highest and lowest, and the average. I got it to get the highest and lowest numbers by fiddling around with the "if" statement and using the ">" and "<" signs.
Eventually I got it to work after trying combination of numbers and declared int statements and was able to get it to define the high and low numbers correctly.
My question is, how does it work?
if (high_num < random_num)
high_num = random_num;
if (low_num > random_num)
low_num = random_num;
I set them to equal to the random number, which I defined later in my int statements. In the "if" statements, it shows the high number being less than a random number, and if it's true, the high number is equal to a random number.... I just don't get how it'd work. Wouldn't it be a greater than sign, which I've tried. All it does is make the lowest 100 and the highest 0.
Ohkay now I'm rambling. Could someone just tell me how that statement works