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++)?
#include <iostream>
usingnamespace 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?
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
#include <iostream>
usingnamespace 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.
#include <iostream>
usingnamespace 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;
}