-
easy newbie question
Hi, I just started taking a class on C++ and I'm a little lost on what looks to be a really easy part of an assignment. I would greatly appreciate it if anyone could help me out. I don't necessarily want you to tell me all the way, but maybe give me a hint.
Any way the assignment is:
"Write a program with a loop that lets the user enter a series of integers. The user should enter –99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered."
I have the input looped and it will stop after you enter -99 that was not a problem, but I can't figure out how to get the smallest and largest numbers out of what was entered. I used a while loop, was that not the way to go or will it work?
Again, any help would be appreciated. Thanks.
-
To get the smallest:
Have a variable(say, int x) to place the smallest and initialized with first input.
Compare the second input with x, if second input smaller, then replace the current value of x with second input
Repeat the last step until you get the invalid input.
Same thing to get the largest
-
Thank you for your reply. I have been working on it for a while and I have gotten this:
Code:
number = 0;
number = max;
number = min;
do
{
cin >> number;
if (number == -99)
break;
if (number > max)
max=number;
else if (number < min)
min=number;
}while (number != -99 );
It works for the most part except the min value is some funky value like 3.58732e-43, it was zero which made sense to me, but I was playing around trying to figure out what I could do to change it and then I get this value instead.
-
You need three variables: The current number entered, the largest, and the smallest. (The problem statement doesn't require you to save all the numbers entered.)
The first time a number is entered the smallest and largest are initialized to the same number:
something like:
Code:
xLarge = X;
xSmall = X;
This probably takes place before you enter the loop.
Then in a loop, something like;
Code:
if ( X > xLarge)
xLarge = X;
if ( X < xSmall )
xSmall = X;
[EDIt]
You have to initialize number with a type, and min & max have to be defined before you use them.
And don't set anything to zero, because the minimum number entered may be greater than zero!
int number, min, max;
And thank's for posting your code!
[EDIT 2]
This won't work: number = min; // We don't want to change "number"
This will work: min = number;
A statement with an equal sign is is not a mathmatical equation... its an assignment statement. It assigns the value on the right to the variable on the left... The variable on the LEFT gets updated. And the variable on the left has to be a single variable:
x = 4 + 4; // OK
x = y + 1; // OK (I edited this it was wrong too!)
x = x + 1; // Same as x++. OK in C++, very bad in math!
x + 2 = 3; // Wrong!
1 = x; // Wrong!
BTW - If your compiler ever gives you an "L-value" error message, this means that you have a problem with the Left side of an assignment statement.
-
Ok, I'm sorry I didn't copy all my code into there.
I'll fix that below, but I'm a little confused, what you said to do seems to me to be exactly what I did, except that I set number=0 which I will take out.
Code:
int main()
{
float number, min, max; // float varialbes
//below is a description/explanation of the program
cout << "Enter number after number into this program for as long as ";
cout << "you would like." << endl;
cout << "When you are done enter -99 and the program ";
cout << "will display the smallest and" << endl;
cout << "largest values you entered." << endl;
cout << ": ";
number = max; //initial value of max
number = min; //initial value of max
do //do while loop for the program and figuring max and min
{
cin >> number;
if (number == -99)
break;
if (number > max)
max=number;
else if (number < min)
min=number;
}while (number != -99 ); //loop for every value except for -99
cout << "The largest value is: " << max << endl; //display largest
cout << "The smallest value is: " << min << endl; //display smallest
getch();
return 0;
}
-
OK, I only see two problems... See my [edits] above. You have your assignments reversed (should be mn = number), and you need a cin >> number before you make that assignment. (I didn't test your code, but thats the only problems I see.)
Code:
//...
cin >> number;
max = number ; //initial value of max
min = number ; //initial value of max
if( number == -99)
return 0; // Just in case user enters -99 as the first number!
do //do while loop for the program and figuring max and min {
//...
-
Ok that worked, the program works great. Rather pointless program, but it works none the less. Thank you very much!
-
Well, 3.58732e-43 is darn close to zero! Yeah, some funky things can happen with floats because of the way they are rounded, and the fact that they are rounded binary numbers makes the rounding un-obvious when the numbers are displayed in decimal. (Everything inside the computer is binary.)
I don't think your program should have these problems because you aren't doing any mathmatical operations... you are just comparing... but you should probably use ints rather than floats.