I am curious as to how I can get a sum of 3 numbers, however it only takes the 2 highest into account. The user inputs 3 numbers and it needs to use only the 2 highest numbers.
for example:
#include "stdafx.h"
#include <iostream>
using namespace std;
float numOne;
float numTwo;
float numThree;
int main()
{
cout << "Enter the first number :";
cin >> numOne;
cout << "Enter the second number :";
cin >> numTwo;
cout << "Enter the third number :";
cin >> numThree;
return 0;
}
Then the code goes on to add the two highest numbers only.
Not asking for someone to give me the answer, rather to guide me through it.
#include <iostream>
int main()
{
// step 1. accept three numbers from the user
int first ;
std::cout << "enter the first number: " ;
std::cin >> first ;
int second ;
std::cout << "enter the second number: " ;
std::cin >> second ;
int third ;
std::cout << "enter the third number: " ;
std::cin >> third ;
// step 2. determine the lowest of these three numbers
int lowest = first ;
if( second < lowest ) lowest = second ;
if( third < lowest ) lowest = third ;
// step 3. find the sum of the two highest numbers
// Q: why does this give us the sum of the two highest numbers?
constint sum_2_highest = first + second + third - lowest ;
// step 4. compute their average note: divide by 2.0 to avoid integer division
constdouble average_2_highest = sum_2_highest / 2.0 ;
// step 5. print the results
std::cout << "sum of the two highest numbers: " << sum_2_highest << '\n'
<< " their average: " << average_2_highest << '\n' ;
}