In: Computer Science
C++
Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs separated by a ';'.
An example of the program is shown below:
list[0] = 15 is the sum of: ---------------------- list[1] = 25 is the sum of: list[5], list[14]; list[13], list[47]; list[46], list[47]; ---------------------- list[2] = 23 is the sum of: list[0], list[33]; ---------------------- list[3] = 65 is the sum of: list[4], list[32]; list[13], list[20]; list[20], list[46]; list[21], list[34]; list[32], list[38]; ---------------------- list[4] = 34 is the sum of: list[0], list[14]; list[6], list[48]; ---------------------- list[5] = 6 is the sum of: ...
If no two elements can equal the value of an element, the line can remain empty, as shown above for list[0] and list[5].
C++ Program:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n=50;
int list[n];
cout << "Enter 50 Integers: ";
for (int i = 0; i < n; i++)
{
cin >> list[i];
}
cout << "\n";
for (int i = 0; i < n; i++)
{
cout<<"list["<<i<<"] = "<<list[i]<<" is the sum of: ";
for (int j = 0; j < n; j++)
{
for (int k = j + 1; k < n; k++)
{
if (list[i] == list[j] + list[k])
{
cout << list[j]<<", "<< list[k] <<" ; ";
}
}
}
cout << endl;
cout << "------------------------------------" << endl;
}
return 0;
}
Sample Output:
...... so more till 50th index, but I added output for the first 14 elements
NOTE: You can use your array, It works appropriately.
Thumbs Up Please !!!