Using If finding the lowest and the highest.

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;

sum = num1 + num2 + num3;
avg = (num1 + num2 + num3) / 3.0;
prod = num1 * num2 * num3;

cout << "The Sum is: " << sum << endl;
cout << "Average is: " << avg << endl;
cout << "Product is: " << prod << endl;




if (num1 > num2)
cout << "Largest " << num1 << endl;
if (num2 > num3)
cout << "Largest " << num2 << endl;
if (num3 > num1)
cout << "Largest " << num3 << endl;



smallest = num1;
if (num2 < smallest)
cout << "Smallest " << smallest << endl;
if (num3 < smallest)
cout << "Smallest " << smallest << endl;



return 0;

}
Your "smallest" starts right.
1
2
3
4
5
6
7
8
9
10
11
12
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.
Last edited on
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.
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
37
38
39
40
41
42
43
44
45
46

#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;

sum = num1 + num2 + num3;
avg = (num1 + num2 + num3) / 3.0;
prod = num1 * num2 * num3;

cout << "The Sum is: " << sum << endl;
cout << "Average is: " << avg << endl;
cout << "Product is: " << prod << endl;




if (num1 > num2)
cout << "Largest " << num1 << endl;
if (num2 > num3)
cout << "Largest " << num2 << endl;
if (num3 > num1)
cout << "Largest " << num3 << endl;



smallest = num1;
if (num2 < smallest)
cout << "Smallest " << smallest << endl;
if (num3 < smallest)
cout << "Smallest " << smallest << endl;



return 0;

}


this code is the original. for reading and runnings
Last edited on
Now here is the code you want gunam12

I have to disagree with that. Lets use input {2,1,3}

In your code:
largest=2
The ( 2 > 1 ) is true, so you print "2"
and therefore the num3 is never compared to anything.
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 

closed account (3qD3AqkS)
Hi, correct me if i am wrong.
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 (){
    
    while (true){
int num1, num2, num3;
double sum, avg, prod;
int smallest = 0;
int largest = 0;

cout << "Input three different integers: "<<endl;
cin >> num1 >> num2 >> num3;

sum = num1 + num2 + num3;
avg = (num1 + num2 + num3) / 3.0;
prod = num1 * num2 * num3;

cout << "The Sum is: " << sum << endl;
cout << "Average is: " << avg << endl;
cout << "Product is: " << prod << endl;

largest = num1;
if (num2 > largest) largest =num2;
if (num3 > largest) largest =num3;
cout <<"largest is "<<largest<<endl;

smallest = num1;
if (num2 < smallest) smallest = num2;
if (num3 < smallest) smallest = num3;
cout<< "smallest is "<<smallest<<endl;
}
return 0;

}


I have took dhayden's idea for the smallest and largest. and i added while true so it could run forever to "compare" numbers
gentlegal, that's fine except perhaps for the product. Consider this run on my 32-bit machine:
Input three different integers:
10000 20000 30000
The Sum is: 60000
Average is: 20000
Product is: -6.93125e+07
largest is 30000
smallest is 10000

The problem is that line 18 computes the integer product and then converts the result to a double to store it in prod.
Topic archived. No new replies allowed.