Hey everyone,
So I'm writing this program that randomly generates 3 numbers between 0 and 100 and then has to display them in order, low to high. I have this so far:
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
usingnamespace std;
// Function prototype
int randomGenerator (int min, int max);
int main()
{
// variable declarations
int minimum = 0;
int maximum = 100;
int num1;
int num2;
int num3;
// call to function randomGenerator
num1 = randomGenerator(minimum, maximum);
num2 = randomGenerator(minimum, maximum);
num3 = randomGenerator(minimum, maximum);
if ( num1 < num2 && num1 < num3 )
{ cout << num1 << endl;
}
elseif ( num2 < num1 && num2 < num3 )
{ cout << num2 << endl;
}
elseif ( num3 < num1 && num3 < num2 )
{ cout << num3 << endl;
}
cout << endl << endl << "Please press enter once or twice to continue...";
cin.ignore().get();
return EXIT_SUCCESS;
}
// Function definition
int randomGenerator (int min, int max)
{
return (min + rand() % (max - min + 1));
}
When I run the program, it just displays one number and not 3. Anybody know what I can do to fix this? I've just started using selection structures so I'm not fluent enough in it yet. Thanks for any help.
I need the randomly generated numbers to be displayed in order of their value, low to high, though. The assignment i'm doing is an exercise in selection control structures, so I have to use if-else statements to display them in order.
int tempValue = 0;
bool swap = true;
while (swap)
{
if (num1 > num2)
{
tempValue = num2; //Store value of num2, since it will be replaced by num1
num2 = num1; //Swap values of num2 and num1
num1= tempValue; //Set num1 to the previous value of num2
swap = true;
}
elseif (num2 > num3)
{
tempValue = num3;
num3 = num2;
num2 = tempValue;
swap = true;
}
else
swap = false; //No swap occurred
}