0

You are given 7 numbers in the form of A, B, C, A+B, B+C, C+A, and A+B+C. You have to make a program in C++ that will be able to figure out the values of A, B, and C given those 7 numbers. Please help fast! I have already created an array to find the max and min of those seven numbers and now I'm stuck.

Here's what I have so far:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
     int arr[10], n=7, i, max, min;
     for (i = 0; i < n; i++)
          cin >> arr[i];
     max = arr[0];
     for (i = 0; i < n; i++)
     {
          if (max < arr[i])
               max = arr[i];
     }
     min = arr[0];
     for (i = 0; i < n; i++)
     {
          if (min > arr[i])
               min = arr[i];
     }
}

Sample input: 2 2 11 4 9 7 9

Output for that input: 2 2 7

Gary Strivin'
  • 908
  • 7
  • 20
  • The input is given in any specified order? – Gary Strivin' Dec 19 '20 at 15:56
  • No, the input is not given in that order, as the order could be mixed around so the first 3 numbers inputted are not always the values of A, B, and C. – smoothieshake Dec 19 '20 at 15:58
  • Are the numbers guaranteed to be positive? – Gary Strivin' Dec 19 '20 at 16:20
  • Yes they are always positive. – smoothieshake Dec 19 '20 at 16:25
  • ⟼This code could benefit greatly by adopting an [indentation style](https://en.wikipedia.org/wiki/Indentation_style) and applying it consistently. Indentation conveys structure and intent which makes it easier for us to understand your code without having to invest a lot of time deciphering it, and it can also make mistakes more obvious as they stand out visually. – tadman Dec 19 '20 at 16:53
  • It would help if you defined your solver in terms of a function with specific inputs, as well as give us an example of inputs and outputs that match properly. – tadman Dec 19 '20 at 16:54

1 Answers1

1

Solution:

Answering to your question "need to create algorithm to sort and output a result", you can use a std::array and the std::sort function from the algorithm library.

However, even if you sort them the three lowest numbers are not guaranteed to be A, B and C as the array could be ordered as A, B, A+B, C, A+C, B+C and A+B+C. You will have to check whether the 3 number in the array is A+B.

Additional information:

  1. using namespace std; is considered a bad practice (More info here).

Full code:

#include <iostream>
#include <array>
#include <algorithm>

int main(){
    constexpr unsigned int n = 7;
    std::array<unsigned int, n> arr;
    for (unsigned int i = 0; i < n; i++)
        std::cin >> arr[i];
    std::sort(arr.begin(),arr.end());
    if(arr[2] == arr[0] + arr[1])
        std::cout << arr[0] << " " << arr[1] << " " << arr[3] << "\n";
    else
        std::cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
}
Gary Strivin'
  • 908
  • 7
  • 20