In: Computer Science
c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that are the sum of two other elements. Use a selection sort algorithm, discussed in this chapter to sort the array. Instructions and code for Programming Exercise 14 have been included for your convenience. Exercise 14 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.
1). ANSWER :
GIVENTHAT :
#include <iostream>
using namespace std;
const int LIST_SIZE = 50;
void selection_sort(int array[], int n)
{
int i, j, min_idx;
for (i = 0; i < n; i++)
{
min_idx = i;//set current element
as minimun index
for (j = i+1; j < n;
j++)
{
if
(array[j] < array[min_idx])//if element at j is less than
element at min position
min_idx = j;//set as min as j
}
// Swap the found
minimum element with the current index element
int
temp=array[min_idx];
array[min_idx]=array[i];
array[i]=temp;
}
}//end selection_sort_acc
int main()
{
int list[LIST_SIZE];
cout << "Enter " << LIST_SIZE
<< " integers: ";
for (int i = 0; i < LIST_SIZE; i++)
cin >>
list[i];
cout << endl;
selection_sort(list,LIST_SIZE);
cout<<"Array after sort
is"<<endl;
for(int i=0;i<LIST_SIZE;i++)
cout<<list[i]<<"
";
cout<<endl;
for (int i = 0; i < LIST_SIZE; i++)
{
cout << "list["
<< i << "] = " << list[i] << " is the sum
of: ";
for (int j = 0; j <
LIST_SIZE; j++)
for (int k = j + 1; k < LIST_SIZE; k++)
if (list[i] == list[j] + list[k])
cout << "list[" << j << "], list[" << k
<< "]; ";
cout <<
endl;
cout <<
"----------------------" << endl;;
}
return 0;
}
/**********************/output
Enter 50 integers: 10 17 23 65 34 6 18 27 35 110 75 25 100 24 19
67 45 88 70 96 41 36 72 150 125 25 77
200 90 166 139 55 31 8 29 119 126 137 34 62 135 121 108 197 45 35
24 1 16 43
Array after sort is
1 6 8 10 16 17 18 19 23 24 24 25 25 27 29 31 34 34 35 35 36 41 43
45 45 55 62 65 67 70 72 75 77 88 90 96 100 108 110 119 121 125 126
135 137 139 150 166 197 200
list[0] = 1 is the sum of:
----------------------
list[1] = 6 is the sum of:
----------------------
list[2] = 8 is the sum of:
----------------------
list[3] = 10 is the sum of:
----------------------
list[4] = 16 is the sum of: list[1], list[3];
----------------------
list[5] = 17 is the sum of: list[0], list[4];
----------------------
list[6] = 18 is the sum of: list[0], list[5]; list[2],
list[3];
----------------------
list[7] = 19 is the sum of: list[0], list[6];
----------------------
list[8] = 23 is the sum of: list[1], list[5];
----------------------
list[9] = 24 is the sum of: list[0], list[8]; list[1], list[6];
list[2], list[4];
----------------------
list[10] = 24 is the sum of: list[0], list[8]; list[1], list[6];
list[2], list[4];
----------------------
list[11] = 25 is the sum of: list[0], list[9]; list[0], list[10];
list[1], list[7]; list[2], list[5];
----------------------
list[12] = 25 is the sum of: list[0], list[9]; list[0], list[10];
list[1], list[7]; list[2], list[5];
----------------------
list[13] = 27 is the sum of: list[2], list[7]; list[3],
list[5];
----------------------
list[14] = 29 is the sum of: list[1], list[8]; list[3],
list[7];
----------------------
list[15] = 31 is the sum of: list[1], list[11]; list[1], list[12];
list[2], list[8];
----------------------
list[16] = 34 is the sum of: list[3], list[9]; list[3], list[10];
list[4], list[6];
----------------------
list[17] = 34 is the sum of: list[3], list[9]; list[3], list[10];
list[4], list[6];
----------------------
list[18] = 35 is the sum of: list[0], list[16]; list[0], list[17];
list[1], list[14]; list[2], list[13]; list[3], list[11]; list[3],
list[12]; list[4], list[7]; list[5], list[6];
----------------------
list[19] = 35 is the sum of: list[0], list[16]; list[0], list[17];
list[1], list[14]; list[2], list[13]; list[3], list[11]; list[3],
list[12]; list[4], list[7]; list[5], list[6];
----------------------
list[20] = 36 is the sum of: list[0], list[18]; list[0], list[19];
list[5], list[7];
----------------------
list[21] = 41 is the sum of: list[1], list[18]; list[1], list[19];
list[3], list[15]; list[4], list[11]; list[4], list[12]; list[5],
list[9]; list[5], list[10]; list[6], list[8];
----------------------
list[22] = 43 is the sum of: list[2], list[18]; list[2], list[19];
list[4], list[13]; list[6], list[11]; list[6], list[12]; list[7],
list[9]; list[7], list[10];
----------------------
list[23] = 45 is the sum of: list[3], list[18]; list[3], list[19];
list[4], list[14]; list[6], list[13];
----------------------
list[24] = 45 is the sum of: list[3], list[18]; list[3], list[19];
list[4], list[14]; list[6], list[13];
----------------------
list[25] = 55 is the sum of: list[3], list[23]; list[3], list[24];
list[7], list[20]; list[9], list[15]; list[10], list[15];
----------------------
list[26] = 62 is the sum of: list[5], list[23]; list[5], list[24];
list[7], list[22]; list[13], list[18]; list[13], list[19];
----------------------
list[27] = 65 is the sum of: list[3], list[25]; list[9], list[21];
list[10], list[21]; list[14], list[20]; list[15], list[16];
list[15], list[17];
----------------------
list[28] = 67 is the sum of: list[9], list[22]; list[10], list[22];
list[15], list[20];
----------------------
list[29] = 70 is the sum of: list[2], list[26]; list[11], list[23];
list[11], list[24]; list[12], list[23]; list[12], list[24];
list[13], list[22]; list[14], list[21]; list[16], list[20];
list[17], list[20]; list[18], list[19];
----------------------
list[30] = 72 is the sum of: list[3], list[26]; list[5], list[25];
list[13], list[23]; list[13], list[24]; list[14], list[22];
list[15], list[21];
----------------------
list[31] = 75 is the sum of: list[2], list[28]; list[3], list[27];
list[16], list[21]; list[17], list[21];
----------------------
list[32] = 77 is the sum of: list[3], list[28]; list[16], list[22];
list[17], list[22]; list[20], list[21];
----------------------
list[33] = 88 is the sum of: list[4], list[30]; list[6], list[29];
list[8], list[27]; list[22], list[23]; list[22], list[24];
----------------------
list[34] = 90 is the sum of: list[6], list[30]; list[8], list[28];
list[11], list[27]; list[12], list[27]; list[18], list[25];
list[19], list[25]; list[23], list[24];
----------------------
list[35] = 96 is the sum of: list[1], list[34]; list[2], list[33];
list[7], list[32]; list[9], list[30]; list[10], list[30]; list[14],
list[28]; list[15], list[27]; list[16], list[26]; list[17],
list[26]; list[21], list[25];
----------------------
list[36] = 100 is the sum of: list[3], list[34]; list[8], list[32];
list[11], list[31]; list[12], list[31]; list[18], list[27];
list[19], list[27]; list[23], list[25]; list[24], list[25];
----------------------
list[37] = 108 is the sum of: list[2], list[36]; list[6], list[34];
list[15], list[32]; list[20], list[30]; list[21], list[28];
list[22], list[27];
----------------------
list[38] = 110 is the sum of: list[3], list[36]; list[18],
list[31]; list[19], list[31]; list[22], list[28]; list[23],
list[27]; list[24], list[27];
----------------------
list[39] = 119 is the sum of: list[7], list[36]; list[8], list[35];
list[14], list[34]; list[15], list[33];
----------------------
list[40] = 121 is the sum of: list[11], list[35]; list[12],
list[35]; list[15], list[34];
----------------------
list[41] = 125 is the sum of: list[1], list[39]; list[5], list[37];
list[11], list[36]; list[12], list[36]; list[14], list[35];
list[18], list[34]; list[19], list[34]; list[25], list[29];
----------------------
list[42] = 126 is the sum of: list[0], list[41]; list[4], list[38];
list[6], list[37]; list[20], list[34];
----------------------
list[43] = 135 is the sum of: list[3], list[41]; list[4], list[39];
list[11], list[38]; list[12], list[38]; list[13], list[37];
list[18], list[36]; list[19], list[36]; list[23], list[34];
list[24], list[34]; list[27], list[29];
----------------------
list[44] = 137 is the sum of: list[4], list[40]; list[6], list[39];
list[13], list[38]; list[14], list[37]; list[21], list[35];
list[26], list[31]; list[27], list[30]; list[28], list[29];
----------------------
list[45] = 139 is the sum of: list[6], list[40]; list[14],
list[38]; list[15], list[37]; list[22], list[35]; list[26],
list[32]; list[28], list[30];
----------------------
list[46] = 150 is the sum of: list[9], list[42]; list[10],
list[42]; list[11], list[41]; list[12], list[41]; list[14],
list[40]; list[15], list[39]; list[26], list[33];
----------------------
list[47] = 166 is the sum of: list[4], list[46]; list[13],
list[45]; list[14], list[44]; list[15], list[43]; list[21],
list[41]; list[23], list[40]; list[24], list[40]; list[29],
list[35];
----------------------
list[48] = 197 is the sum of: list[15], list[47]; list[26],
list[43]; list[30], list[41];
----------------------
list[49] = 200 is the sum of: list[16], list[47]; list[17],
list[47]; list[27], list[43]; list[31], list[41]; list[34],
list[38];