I can't get the Smallest and Largest to display properly.
I'm only using "If" because the book only been using If statement.
i got other to work properly.
Input three different integers: 13 27 14
Sum is 54
Average is 18
Product is 4914
Smallest is 13
Largest is 27
[code]
#include <iostream>
using namespace std;
int main ()
{
int num1, num2, num3;
double sum, avg, prod;
int smallest = 0;
int largest = 0;
cout << "Input three different integers: ";
cin >> num1 >> num2 >> num3;
smallest = num1; // ok, first assume that num1 is smallest
if (num2 < smallest) // ok, test assumption and prepare to improve it
cout << "Smallest " << smallest << endl; // no, we don't know the smallest value yet
// you should update the assumption, smallest = ...
if (num3 < smallest) // same as with num2
cout << "Smallest " << smallest << endl;
// after all values have been studied we finally know the smallest
// and can show it HERE
The largest is resolved with same principle.
PS. sum and product can be integers. There is no reason to lose exactness with the use of floating point types for them.
sum and product can be integers.
Personally, I would keep prod as a double but change sum to an int. With a 32 bit int, the product of three medium sized numbers like 5,000, 6,000 and 7,000 will overflow int's capacity.
Thedudeplaysmc 3, I think you've misinterpreted Keskiverto's comment. I doubt that any insult was intended. That user is one of the most helpful and prolific users on this forum (check the post count).
To find the smallest with only if statements, you could do this:
1 2 3
int smallest = num1;
if (num2 < smallest) smallest = num2; // smallest is now smallest of num1 & num2
if (num3 < smallest) smallest = num3; // smallest is now smallest of num1, num2 & num3