Reputation: 4444
How to write c++ program that will let user enters three numbers, and find between that three 2 higest and write smth like : 5,3,4 sum of 5 and 4 is 9!
I tried allready, but I get too many if elses and it looks really bad, and even not working well in all cases :/
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
if(n1 >= n2 && n1 >= n3)
{
if(n2>=n1 && n2>=3)
cout << "Sum of 2 highest between this three numbers is: " << cout<<n1+n2;
}
if(n1 >= n2 && n1 >= n3)
{
if(n3>=n1 && n3>=n2)
cout << "Sum of 2 highest between this three numbers is: " << cout<<n1+n3;
}
if(n2 >= n1 && n2 >= n3)
{
if(n3 >= n1 && n3 >= n2) {
cout << "Sum of 2 highest between this three numbers is: " << cout<<n3+n2;
}
}
if(n3 >= n1 && n3 >= n2)
{
if(n2 >= n1) {
cout << "Sum of 2 highest between this three numbers is: " << cout<<n3+n2;
}
}
if(n3 >= n1 && n3 >= n2)
{
if(n1>= n2) {
cout << "Sum of 2 highest between this three numbers is: " << cout<<n3+n1;
}
}
return 0;
}
Upvotes: 0
Views: 413
Reputation: 36607
A simple approach to answer the question as asked would be
cout << "Sum of 2 highest between this three numbers is: ";
cout << (long long) n1 + n2 + n3 - std::min(n1, std::min(n2, n3));
or (C++11)
cout << "Sum of 2 highest between this three numbers is: ";
cout << (long long) n1 + n2 + n3 - std::min({n1, n2, n3});
The (long long)
conversion forces all values being added or subtracted to be promoted before the operation, to avoid overflow if int
supports a smaller range of values than a long long
.
Of course, this does embed a fixed assumption of three values being worked with.
Upvotes: 0
Reputation: 56557
Find the minimum first,
double min = std::min(n1, std:min(n2, n3)); // or std::min({n1, n2, n3})
then eliminate it
if(min == n1)
{
std::cout << n2 << ' ' << n3 << ' ' << n2 + n3;
}
else if(min == n2)
{
std::cout << n1 << ' ' << n3 << ' ' << n1 + n3;
}
else
{
std::cout << n1 << ' ' << n2 << ' ' << n1 + n2;
}
Of course if you're really picky you can replace the call to std::min
by
double min = n1;
if(n2 < min)
min = n2;
if(n3 < min)
min = n3;
Another solution is to just do the min finding and elimination in a single step, like
if(n1 < n2 && n1 < n3) // min is n1
{
std::cout << n2 << ' ' << n3 << ' ' << n2 + n3;
}
else if(n2 < n1 && n2 < n3) // min is n2
{
std::cout << n1 << ' ' << n3 << ' ' << n1 + n3;
}
else // min is n3
{
std::cout << n1 << ' ' << n2 << ' ' << n1 + n2;
}
In terms of complexity, both solutions are identical, i.e. require 4 comparisons.
Upvotes: 2
Reputation: 106
You only need to do a few checks to get the desired result.
int n1 = 0,n2 = 0,n3 = 0;
cin>>n1>>n2>>n3;
int highest = 0;
int second_highest = 0;
if(n1 > n2)
{
if(n1 > n3)
{
highest = n1;
if(n3 > n2)
{
second_highest = n3;
}
else
{
second_highest = n2;
}
}
else
{
highest = n3;
second_highest = n1;
}
}
else if(n2 > n3) // if the first if is false it means that the n2 is highest or second highest
{
highest = n2;
if(n1 > n3)
{
second_highest = n1;
}
else
{
second_highest = n3;
}
}
else
{
highest = n3;
second_highest = n2;
}
cout << "Highest: " << highest << "\nSecond highest: " << second_highest;
Hope that helps.
Upvotes: 0
Reputation: 36
Try this:
max = (n1>n2) ? ( (n1>n3) ? n1 : n3) : ((n2>n3) ? n2 : n3);
second = (n1==max) ? ( (n2>n3) ? n2 : n3) : ((n2==max) ? ((n1>n3) ? n1 : n3) : ((n2>n1)? n2 : n1));
Upvotes: 0