Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using C++ Selection Sort Populate an array randomnums of size 20 with integers r

ID: 3695619 • Letter: U

Question

using C++

Selection Sort Populate an array randomnums of size 20 with integers read from an input file numbers.txt. Whether the file is generated automatically by another program or being created manually is up to you. But your program SHOULD NOT overwrite an existing file numbers.txt if there is one. The format of the file is one number in each row. The testing input file will contain 20 integers in the interval [0, 50]. Your program should do the following: Use C-style array rather than STL vector. No global variables are allowed. Print the elements of the array randomnums after it is populated. Perform selection sort using your own function selectsort_ascending on array randomnums, and sort the array in non-decreasing order. Print the elements of randomnums after sorting in main function. Create another array terminalsum of size 10. Initialize each element of terminalsum by adding each pair of terminal elements from randomnums. For Example: terminalsum 1^st element = randomnums 1^st element + randomnums 20st element terminalsum 2^nd element = randomnums 2^nd element + randomnums 19th element terminalsum 10^th element = randomnums 10^th element + randomnums 11^st element Print terminalsum array after initialization. Perform selection sort on terminalsum, and sort the array in non-increasing order using another function selectsort_descending. Print the elements of the array terminalsum after sorting in main function. Ensure that the output displayed on the terminal screen is interpretable.

Explanation / Answer

/** C+ code to manually create a text file with 20 integers and sort numbers using selection sort in increasing and decreasing order **/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;


void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void selectionSort_asending(int arr[], int n)
{
int i, j, min_idx;

// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}

void selectionSort_descending(int arr[], int n)
{
int i, j, min_idx;

// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] > arr[min_idx])
min_idx = j;

// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}

/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf(" ");
}

int main()
{
int randomnums[20];
ifstream file_("numbers.txt"); // opening numbers.txt

int number;
int i = 0;

while(file_ >> number)
{
randomnums[i] = number;
i++;
}
  
file_.close(); // closing numbers.txt

printf("Unsorted array ");
printArray(randomnums,20);


selectionSort_asending(randomnums,20);
printf(" Sorted in asending order ");
printArray(randomnums,20);

int j = 0;
int terminalsum[10];
for ( i = 0; i < 10; ++i)
{
terminalsum[j] = randomnums[i]+randomnums[20-i-1];
j++;
}

printf(" terminalsum array ");
printArray(terminalsum,10);
selectionSort_descending(terminalsum,10);
printf(" Sorted in decreasing order ");
printArray(terminalsum,10);

return 0;
}