In C++ Populate an array of size 100 with integers > = 0 and < = 1000. Initializ
ID: 3813932 • Letter: I
Question
In C++
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. Name the array RandomNums.
Further,
1. Print the elements of the array RandomNums after initialization.
2. Perform a bubble sort on RandomNums , and sort the array in decreasing order.
3. Print the elements of RandomNums after sorting.
4. Create another array of size 50, name it as TerminalSum
5. Initialize each element of TerminalSum by adding each pair of terminal elements from RandomNums .
For Example:
TerminalSum 1s t element = RandomNums 1s t element + RandomNums 100s t element
TerminalSum 2n d element = RandomNums 2n d element + RandomNums 99t h element
TerminalSum 50t h element = RandomNums 50t h element + RandomNums 51s t element
Print TerminalSum array after initialization.
6. Perform a selection sort on TerminalSum , and sort the array in increasing order
7. Print the elements of the array TerminalSum after sorting.
Example:
The contents of RandomNums: 78, 34, 56......................
RandomNums after sorting in the decreasing order: 43, 56, 78.................
Contents of TerminalSum: 120, 78, 112......
TerminalSum after sorting in the increasing order: 78, 112, 120
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int arr[100];
int t[50];
srand((unsigned)time(0));
for(int i=0; i<100; i++)
{
arr[i] = (rand()%100)+1;
}
cout<<"The contents of Random Nums :" <<endl;
for(int j=0 ;j<100;j++)
{
cout<<arr[j]<<" ";
}
cout<<endl;
int temp;
for(int k=0;k<=(100-1);k++)
{
for(int l=0;l<(100-k-1);l++)
if(arr[l]<arr[l+1])
{
temp=arr[l];
arr[l]=arr[l+1];
arr[l+1]=temp;
}
}
cout<<"Random array after sorting in decreasing order"<<endl;
for(int m=0;m<100;m++)
{
cout<<arr[m]<<" ";
}
cout<<endl;
cout<<"Contents of TerminalSum"<<endl;
for(int n=0,p=99;p<=50;n++,p--)
{
t[n] = arr[n]+ arr[p];
cout<<t[n]<<" ";
}
cout<<endl;
int tempr;
for (int q= 1; q < 50; q++)
{
for (int r = q; r >= 1; r--)
{
if (t[r] < t[r-1])
{
tempr = t[r];
t[r] = t[r-1];
t[r-1] = tempr;
}
else
break;
}
}
cout<<"TerminalSum after sorting in increasing order"<<endl;
for(int s=0;s<50;s++)
{
cout<<t[s]<<" ";
}
return 0;
}