USING C++ Programming WIth Selection Sort. Populate an array of size 100 with in
ID: 3768469 • Letter: U
Question
USING C++ Programming
WIth Selection Sort.
Populate an array of size 100 with integers > = 0 and < = 1000. Initialize the array using a random number generator with “time” as the seed value., call it RandomNums.
Further,
1. Print the elements of the array RandomNums after initialization.
2. Perform selection sort using function on array RandomNums, and sort the array in non-increasing order.
3. Print the elements of RandomNums after sorting from within the above defined function.
4. Create another array of size 50, call it TerminalSum
5. Initialize each element of TerminalSum by adding each pair of terminal elements from RandomNums.
For Example:
TerminalSum 1st element = RandomNums 1st element + RandomNums 100st element
TerminalSum 2nd element = RandomNums 2nd element + RandomNums 99th element
.
.
TerminalSum 50th element = RandomNums 50th element + RandomNums 51st element
Print TerminalSum array after initialization.
6. Perform selection sort on TerminalSum, and sort the array in non-decreasing order using another function.
7. Print the elements of the array TerminalSum after sorting from within the above defined function.
Note: Ensure that the outputs displayed on the terminal screen are interpretable
Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define data_size 100
#define pair_size data_size/2
void print(int *RandomNums,int size)
{
int i;
for(i=0;i<size;i++)
cout<<RandomNums[i]<<" ";
cout<<endl;
}
void selectSort_desc(int* arr, int n)
{
//pos_min is short for position of min
int pos_min,temp;
for (int i=0; i < n-1; i++)
{
pos_min = i;//set pos_min to the current index of array
for (int j=i+1; j < n; j++)
{
if (arr[j] > arr[pos_min])
pos_min=j; //pos_min will keep track of the index that min is in, this is needed when a swap happens
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
temp = arr[i];
arr[i] = arr[pos_min];
arr[pos_min] = temp;
}
}
}
void selectSort_asce(int* arr, int n)
{
//pos_min is short for position of min
int pos_min,temp;
for (int i=0; i < n-1; i++)
{
pos_min = i;//set pos_min to the current index of array
for (int j=i+1; j < n; j++)
{
if (arr[j] < arr[pos_min])
pos_min=j; //pos_min will keep track of the index that min is in, this is needed when a swap happens
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
temp = arr[i];
arr[i] = arr[pos_min];
arr[pos_min] = temp;
}
}
}
int main()
{
srand(time(NULL));
int i,RandomNums[data_size],TerminalSum[pair_size];
for(i=0;i<data_size;i++)
RandomNums[i] = rand()%1001;
printf("RandomNums before selection sort ");
print(RandomNums,data_size);
selectSort_desc(RandomNums,data_size);
printf("RandomNums after selection sort (Non increasing) ");
print(RandomNums,data_size);
for(i=0;i<pair_size;i++)
TerminalSum[i] = RandomNums[i]+RandomNums[data_size-1-i];
printf("TerminalSum before selection sort ");
print(TerminalSum,pair_size);
selectSort_asce(TerminalSum,pair_size);
printf("TerminalSum after selection sort(Non descreasing) ");
print(TerminalSum,pair_size);
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.