C is the language Populate an array randomNums of size 50 w
C++ is the language
.
Populate an array randomNums of size 50 with random integers in the range 0-500. Further,
a) Print the elements of the array randomNums after initialization.
b) Perform selection sort on randomNums, and sort the array in increasing order.
c) Print the elements of randomNums after sorting.
d) Create another array terminalSum of size 25.
e) Initialize each element of terminalSum by adding each pair of terminal elements from randomNums. Print terminalSum array after initialization.
For Example:
terminalSum 1st element = randomNums 1st element + randomNums 50th element.
terminalSum 2nd element = randomNums 2nd element + randomNums 49th element.
.
terminalSum 25th element = randomNums 25th element + randomNums 26th element.
f) Perform selection sort on terminalSum, and sort the array in decreasing order
g) Ask the user for a number (0-500) and binary search it in the sorted terminalSum. Report the index location if found else report not found.
h) Print the elements of the array terminalSum after sorting.
Solution
#include<iostream>
#include<stdlib.h>
using namespace std;
void swap(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
}
int selectionSortAsc(int *arr,int size)
{
int position;
for (int i = 0; i < size-1; i++)
{
position = i;
for (int j = i+1; j<size; j++)
{
if (arr[position] > arr[j])
position = j;
}
if (position != i)
{
swap(arr[i],arr[position]);
}
}
}
int selectionSortDesc(int *arr,int size)
{
int position;
for (int i = 0; i < size-1; i++)
{
position = i;
for (int j = i+1; j<size; j++)
{
if (arr[position] < arr[j])
position = j;
}
if (position != i)
{
swap(arr[i],arr[position]);
}
}
}
int binarySearch(int *arr,int low,int high,int key)
{
if(low>high)
return -1;
else
{
int middle = (low+high)/2;
if(arr[middle] == key)
return middle;
else if(arr[middle]>key)
return binarySearch(arr,middle+1,high,key);
else
return binarySearch(arr,low,middle-1,key);
}
}
void printArray(int *arr,int size)
{
for(int i=0;i<size;i++)
cout << arr[i] << \" \" ;
cout << endl;
}
int main()
{
int arr[50];
int terminalSum[25];
for(int i=0;i<50;i++)
arr[i] = rand()%501;
cout << \"Array Before sorting : \";
printArray(arr,50);
selectionSortAsc(arr,50);
cout << \"Array After sorting : \";
printArray(arr,50);
int i=0,j=49;
int k = 0;
while(i<j)
{
terminalSum[k] = arr[i]+arr[j];
i++;
j--;
k++;
}
cout << \"Array Before sorting : \";
printArray(terminalSum,25);
selectionSortDesc(terminalSum,25);
cout << \"Array After sorting : \";
printArray(terminalSum,25);
cout << \"Input an element to search in Terminal Sum : \";
int key;
cin >> key;
int index = binarySearch(terminalSum,0,24,key);
if(index == -1)
cout << \"Element not found. \" << endl;
else
cout << \"Element found at \" << index << endl;
return 0;
}
OUTPUT:
Array Before sorting : 7 406 105 487 113 52 490 57 498 388 50 439 80 421 131 253 129 309 208 28 341 260 339 411 194 425 29 226 283 100 299 36 6 150 269 366 202 259 169 199 393 219 384 473 386 262 473 14 317 180
Array After sorting : 6 7 14 28 29 36 50 52 57 80 100 105 113 129 131 150 169 180 194 199 202 208 219 226 253 259 260 262 269 283 299 309 317 339 341 366 384 386 388 393 406 411 421 425 439 473 473 487 490 498
Array Before sorting : 504 497 501 501 502 475 475 473 468 486 493 493 499 513 497 491 508 497 503 498 485 477 481 486 512
Array After sorting : 513 512 508 504 503 502 501 501 499 498 497 497 497 493 493 491 486 486 485 481 477 475 475 473 468
Input an element to search in Terminal Sum : 501
Element found at 6


