Question

In: Computer Science

c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that...

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.

Solutions

Expert Solution

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];


Related Solutions

C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by overloading the operators as nonmembers of the class rectangleType. The header and implementation file from Exercise 1 have been provided. Write a test program that tests various operations on the class rectangleType. I need a main.cpp file Given: **************rectangleType.cpp******************** #include <iostream> #include <cassert> #include "rectangleType.h" using namespace std; void rectangleType::setDimension(double l, double w) { if (l >= 0) length = l; else length =...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements....
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements. Fill the array with random integers (use a loop). Neatly output each element in the one-dimensional array. Next convert your one-dimensional array of 24 elements into a two-dimensional array of 6 x 4 elements. Neatly output each element of the two-dimensional array. The values will be identical to the one-dimensional array – you’re just converting from one dimension to two.
Write a C function to swap the first and last elements of an integer array. Call...
Write a C function to swap the first and last elements of an integer array. Call the function from main() with an int array of size 4. Print the results before and after swap (print all elements of the array in one line). The signature of the arrItemSwap() function is: void arrItemSwap(int *, int, int); /* array ptr, indices i, j to be swapped */ Submit the .c file. No marks will be given if your pgm does not compile...
Consider sorting n numbers stored in array A by first finding the smallest element of A...
Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A and exchange it with A[2]. Continue in this manner for the first n-1 elements of A. a) (10 points) This algorithm is known as the selection sort. Write the pseudo code for this algorithm. b) (10 points) What loop invariant does this algorithm maintain? Note: You do not...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
This is C++ programing Reversing the elements of an array involves swapping the corresponding elements of...
This is C++ programing Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array.Given an array a, an int variable n containing the number of elements in a, and two other intvariables, k and temp, write a loop that reverses the elements of the array.Do not use any other variables besides...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. It is possible that the smallest numbers are repeated – you should print the number of occurrences of the two smallest numbers....
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist in the former array. They need not necessarily be in the same order. For example, [1,7,3,4,2] overlaps [1,2,3] because 1,2 and 3 exist in [1,7,3,4,2]. To make the implementation easy, [1,7,3,4,2] overlaps [1,1,2,3] as well. We don’t need to check whether 1 appears twice in the first array. Write a program that lets the user enter two arrays and displays whether the first array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT