# Saving the largest and smallest numbers.

• 12-06-2007
Sembhi
Saving the largest and smallest numbers.
I have a program where the user has to input numbers over and over, if they want to end the program they hit 0. After they hit zero though I have to display the largest and smallest numbers they entered. Tips on how I can do this?
• 12-06-2007
MacGyver
I find it hard to believe you can't come up with a way to do this without someone giving you an algorithm. How about you try to come up with a way to do this first? If it seems reasonable, and it makes sense, then try coding it.
• 12-06-2007
Sembhi
What I had in mind was using if, setting the first number entered to a variable called temporary, and have all the numbers entered compared to that, if the number entered bigger/smaller have that number become temporary. Do you think that will work?

Yea I'm confused. This is my code.

Code:

```#include <iostream.h> int main() {         int sum = 0;         int num;         int total = 0;         int temp = 0;         int largest;         do         {                 cout << "Enter a number (Enter 0 to quit): ";                 cin >> num;                         for (temp = num)                 {                         if (num > temp)                         largest = temp;                 }                         total = total + num;                         if (num == 0)                                 {                 cout << largest;                 cout << total << " ";                 break;                 }                         }         while (1);         return 0; }```
Getting two errors

Quote:

--------------------Configuration: 6.1 - Win32 Debug--------------------
Compiling...
6.1.cpp
D:\P2\C++\6.1.cpp(27) : error C2143: syntax error : missing ';' before ')'
D:\P2\C++\6.1.cpp(27) : error C2143: syntax error : missing ';' before ')'
Error executing cl.exe.

6.1.obj - 2 error(s), 0 warning(s)
• 12-06-2007
fl00d
Quote:

Originally Posted by Sembhi
What I had in mind was using if, setting the first number entered to a variable called temporary, and have all the numbers entered compared to that, if the number entered bigger/smaller have that number become temporary. Do you think that will work?

Well if it's going to output BOTH the smallest and biggest, you'll probably need 2 temp variables. And yeah that method could work.
• 12-06-2007
Sembhi
Yea I know, I'm just trying to get one to work first.

Can someone take a look at the code posted above and tell me whats wrong with it?
• 12-06-2007
fl00d
Quote:

for (temp = num)
{
if (num > temp)
largest = temp;
}
wrong syntax for a for statement.
ex:

Code:

```for(int a=0;a<=num;a++) { }```
• 12-06-2007
Sembhi
^ Thanks.

Code:

```#include <iostream.h> int main() {         int sum = 0;         int num;         int total = 0;         int temp = 0;         int largest = 0;         do         {                 cout << "Enter a number (Enter 0 to quit): ";                 cin >> num;                         for(int temp=0;temp<=num;temp++)                         {                 if (num > temp)                 largest = temp;                 }                         total = total + num;                         if (num == 0)                                 {                 cout << largest;                        cout << total << " ";                 break;                 }                         }         while (1);         return 0; }```
Why is largest not displaying?
• 12-06-2007
Desolation
Most probably because num does not equal zero ?
• 12-06-2007
Sembhi
Then why does the total display?
• 12-06-2007
clegs
Why are you using temp as you iterator and also compare it to num?
• 12-06-2007
Elysia
clegs is correct. Your for loop is quite flawed and not necessary.
Let me ask you: what is your code supposed to do? What is the "algorithm" you have in mind to do this? It's a very simple project, but your code is all wrong.
Can you clarify how you're thinking you can do this? If you don't have an idea on the flow of your application, you need to think of one. As I like to say, re-think your design! That is, if your current design you have in mind is not correct.
• 12-07-2007
Cat
Yeah, the inner loop is completely unnecessary.

Can you try to describe, in words, what you're trying to do? Assume you have a variable named 'largest' that holds the largest number previously found, and assume you have a variable called 'num' which holds the most recent number. How do you decide whether to update 'largest' or not?