0
#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

Edited by vader1231231: n/a

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Narue
0

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;

You did write the code yourself didn't you??

Take a closer look at the code, read it aloud if it helps..
if high num is less than random num (therefore random num is greater than high num):
set high num to the value of random num

if low num is greater than random num (therefore random num is lower than low num):
set low num to be equal to the value of random num

Does that clarify things?

[edit: see below:]
BTW, This is another more intuitive way of doing exactly the same thing:

if (random_num > high_num)
        high_num = random_num;
        
        if (random_num < low_num)
        low_num = random_num;

Cheers for now,
Jas.

Edited by JasonHippy: added some more

-1

oh yeah makes sense now, thanks :)

I did write it myself, like I said, I just kinda fiddled with different combinations till I got it to work.

0

>I just kinda fiddled with different combinations till I got it to work.
That's called programming by accident, and it means you're no better than an ape pounding at the keyboard. There's no design involved, you run an even higher risk of introducing bugs into code you don't understand, it's virtually impossible to verify correctness, and it takes longer to reach a stable point (if you ever reach one).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.