Largest/Smallest

Okay so I'm reading this book I've had for quite a few years.."How to program c++" by Deitel, sixth edition.

This exercise is one of the first few the author ask the reader to do..write a program to produce the sum, average, product, smallest, and largest of 3 integers. Now I want to assume that the author wants us to make this program with the knowledge given to the reader in the previous pages..

So how exactly would one go about finding the smallest and largest number of 3 integers without using a much more complex line of code..such as for(int i=0;i<length;i++)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

using namespace std;

int main ()
{
	int x;
	int y;
	int z;

	cout << "Input three different integers: ";
	cin >> x >> y >> z;
	cout << "Sum is " << x+y+z << endl;
	cout << "Average is " << (x+y+z)/3 << endl;
	cout << "Product is " << x*y*z << endl;
	if ( x < y && x < z )
		cout << "Smallest is " << x << endl;
	if ( y < x && y < z )
		cout << "Smallest is " << y << endl;
	if ( z < x && z < y )
		cout << "Smallest is " << z << endl;
	if ( x > y && x > z )
		cout << "Largest is " << x << endl;
	if ( y > x && y > z )
		cout << "Largest is " << y << endl;
	if ( z > x && z > y )
		cout << "Largest is " << z << endl;
	system ("Pause");
	return 0;
}
// Input three different integers: 1 2 3
// Sum is 6
// Average is 2
// Product is 6
// Smallest is 1
// Largest is 3 

This code prints out the correct numbers, but I feel like there are too many lines to express it, what else could be used?
To find largest number:

1
2
if (num > largesttNum) //checks to see if the number recently input is greater than the previous number
largestNum = num; //if the number input is greater, then it is transferred to the largestnum variable 


It is somewhat a similar process to find the smallest number
Last edited on
Can't say that I currently understand fully, but Ill look more into it. :)
Well unfortunately, that code is used with a loop. You can check the code out below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
    int num;
    int largestNum = 0, counter = 0;

    while(counter < 5) //the below info will loop until counter equals 5
    {
    cout << "Enter a number: ";
    cin >> num;
    if(num > largestNum) //checks to see if last number is the largest
        {
            largestNum = num; //if number was larger, largestNum is updated with largest number
        }
    cout << endl;
    counter++; //adds one to the variable counter
    }

    cout << "The largest number is " << largestNum << endl << endl;
return 0;
}


I have just learned this not too long ago and this is the best way I can find the largest/ smallest number. There are sure to be other ways though
Okay that helps me understand where you were getting the
1
2
if (num > largesttNum) 
largestNum = num
part from a little more. But like you said, in that formal it will be a loop, so ill tweak it for now, but still looking for different options as I'd like to figure out more methods..but thanks! was helpful.
Any other suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 #include <iostream>

using namespace std;

int main ()
{
	int x;
	int y;
	int z;
	int largestNum = 0;

	cout << "Input three different integers: ";
	cin >> x >> y >> z;
	cout << "Sum is " << x+y+z << endl;
	cout << "Average is " << (x+y+z)/3 << endl;
	cout << "Product is " << x*y*z << endl;
	if ( x < y && x < z )
		cout << "Smallest is " << x << endl;
	if ( y < x && y < z )
		cout << "Smallest is " << y << endl;
	if ( z < x && z < y )
		cout << "Smallest is " << z << endl;
	if( (x || y || z) >= largestNum)                       // added line only counts z as the largest number, even if  x or y is bigger.
{
   largestNum = (x,y,z);
   cout << "Largest is "  << largestNum << endl;
}

	system ("Pause");
	return 0;
}
Last edited on
Topic archived. No new replies allowed.