Reputation: 1
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
Upvotes: 0
Views: 48
Reputation: 908
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:
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";
}
Upvotes: 1