Average using highest numbers

Jun 26, 2018 at 3:57am
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.
Last edited on Jun 26, 2018 at 4:02am
Jun 26, 2018 at 5:54am
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
#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?
    const int sum_2_highest = first + second + third - lowest ;

    // step 4. compute their average note: divide by 2.0 to avoid integer division
    const double 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' ;
}
Topic archived. No new replies allowed.