# Thread: Determine the largest and the smallest

1. ## Determine the largest and the smallest

As title, I'm just a new C++ beginner (a.k.a noob), currently I have an exercise to determine the largest and the smallest from 3 different integers.

Code:
```// Read three different integers and hence determine the largest and the smallest integer
#include <iostream> // program performs input and output

using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl

// function main begins program execution
int main()
{
int x; // first input integer
int y; // second input integer
int z; // third input integer

cout << "Input three different integers: "; // prompt user for data
cin >> x >> y >> z; // read three integers from user

if ( x < y )
cout << "Smallest is " << x << endl;
if ( x < z )
cout << "Smallest is " << x << endl;
if ( y < x )
cout << "Smallest is " << y << endl;
if ( y < z )
cout << "Smallest is " << y << endl;
if ( z < x )
cout << "Smallest is " << z << endl;
if ( z < y )
cout << "Smallest is " << z << endl;

return 0; // indicate program executed successfully

} // end function main```
With this, I got the result as below when I entered 13, 27 and 14 :
Code:
```Smallest is 13
Smallest is 13
Smallest is 14```
Instead, the result suppose to be like this (forgive me for skipping the parts looking for the largest number cuz it didn't work at this moment):
Code:
```Smallest is 13
Largest is 27```
Geez, I'm stuck in this just at the beginning of my C++ programming learning, can anybody guide me and show me where I wrong. So far, I'd only know how to use the 'if' statement and those binary and relational operators including equality operators. Thanks

2. You have to use an additional variable, say smallest.
After input of all 3 variables you assign it eg. the value of x.
After that you need just 2 if statements like
Code:
`if ( y < smallest ) smallest = y;`
After that you just print smallest.
Kurt

3. Code:
```int x, y, z;
int smallest, largest;

cout << "Input three different integers: "; // prompt user for data
cin >> x >> y >> z; // read three integers from user

smallest = x;
largest = x;

if(y < smallest)
smallest = y;

if(z < smallest)
smallest = z;

if(y > largest)
largest = y;

if(z > largest)
largest = z;

cout << "Smallest is: " << smallest << endl;
cout << "Largest is: " << largest << endl;```

4. Originally Posted by ZuK
You have to use an additional variable, say smallest.
After input of all 3 variables you assign it eg. the value of x.
After that you need just 2 if statements like
Code:
`if ( y < smallest ) smallest = y;`
After that you just print smallest.
Kurt
Originally Posted by 39ster
Code:
```int x, y, z;
int smallest, largest;
cout << "Input three different integers: "; // prompt user for data
cin >> x >> y >> z; // read three integers from user
smallest = x;
largest = x;
if(y < smallest)
smallest = y;
if(z < smallest)
smallest = z;
if(y > largest)
largest = y;
if(z > largest)
largest = z;
cout << "Smallest is: " << smallest << endl;
cout << "Largest is: " << largest << endl;```
Wow, amazing, I'm studying on my own and this is the last question I need to solve in chapter 2. However, the book didn't teach me much on the skill like yours. Thanks for your helps

5. Another way would be to store all three numbers in an array and periodically search for the highest and smallest numbers in the array. It requires less code.
Does any of you know how to use arrays?

6. Originally Posted by Elysia
Does any of you know how to use arrays?
No I dont. Please tell me.
Kurt

7. Very funny. Wasn't referring to you.