C Populate an array randomNums of size 50 with random intege
C++
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 1 st element = randomNums 1 st element + randomNums 50th element.
terminalSum 2 nd element = randomNums 2 nd 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 <time.h>
 using namespace std;
 const int SIZE=10;
 //display array elements
 void print(int array[],int n)
 {
     for(int i=0;i<n;i++)
     {
         if(i%10==0)
         cout<<\"\ \";
         cout<<array[i]<<\"\\t\";
     }
 }
//sort array elemnsts using selection sort in aescending order
 void selectionSortAsc(int array[],int n)
 {
     //initilaize variables
     int min=999,loc=-1,temp;
     for(int i=0;i<n-1;i++)
     {
              //assign array elem,nt to max variable and keep track of location
         min=array[i];
         loc=i;
           //search for elemnt greater than min
         for(int j=i+1;j<n;j++)
         {
           
             if(min>array[j])
             {
                 min=array[j];
                 loc=j;
             }
         }
         //exchange the positions of each smallest element found
 
         temp=array[i];
         array[i]=array[loc];
         array[loc]=temp;
     }
 }
//sort array elemnsts using selection sort in aescending order
 void selectionSortDsc(int array[],int n)
 {
     //initilaize variables
     int max=-1,loc=-1,temp;
     //loop through the array
     for(int i=0;i<n-1;i++)
     {
         //assign array elem,nt to max variable and keep track of location
         max=array[i];
         loc=i;
         //search for elemnt greater than max
         for(int j=i+1;j<n;j++)
         {
             if(max<array[j])
             {
                 max=array[j];
                 loc=j;
             }
         }
 
 //exchange positions of each greater elemtent found
         temp=array[i];
         array[i]=array[loc];
         array[loc]=temp;
     }
 }
 //do binary searc for array sorted in descedening
 int binarySearch(int array[], int lowerbound, int upperbound, int key)
 {
        int position;
       // To start, find the subscript of the middle position.
        position = ( lowerbound + upperbound) / 2;
       while((array[position] != key) && (lowerbound <= upperbound))
        {
               if (array[position] < key)               // If the number is > key, ..
              {                                                       // decrease position by one.
                     upperbound = position - 1;  
              }                                                    
               else                                              
              {                                                        // Else, increase position by one.
                    lowerbound = position + 1;   
              }
              position = (lowerbound + upperbound) / 2;
        }
       if (lowerbound <= upperbound)
       {
             return position;            
         }    
        else
        return -1;
 }
 int main()
 {
     int randomNums [SIZE];
     //intialzie randomNums array
     srand (time(NULL));
     for(int i=0;i<SIZE;i++)
     {
         randomNums[i]=rand() % 500 ;
     }
     cout<<\"\ Elements in randomNums array\";
     //print randomNums array before sorting
     cout<<\"\ Before Sorting\";
     print(randomNums,SIZE);
     //sort elements in randomNums array
     selectionSortAsc(randomNums,SIZE);
     //print randomNums array after sorting
     cout<<\"\ After sorting\";
     print(randomNums,SIZE);
   
     cout<<\"\ Elements in terminalSum array\";
     int terminalSum [SIZE/2];
     //intialzie randomNums array
     for(int i=0;i<=SIZE/2;i++)
     {
        terminalSum[i]= randomNums[i]+randomNums[SIZE-1-i];
     }
     //print terminalSum array before sorting
     cout<<\"\ Before Sorting\";
     print(terminalSum,SIZE/2);
     //sort elements in terminalSum array
     selectionSortDsc(terminalSum,SIZE/2);
      //print terminalSum array after sorting
      cout<<\"\ After sorting\";
     print(terminalSum,SIZE/2);
   
     //ask user for input
     int choice;
     cout<<\"\ Enter value between 0-500: \";
     cin>>choice;
     if(choice>=0 && choice<=500)
     {
         int index=binarySearch(terminalSum,0,SIZE/2,choice);
         if(index==-1)
         cout<<\"Number \"<<choice<<\" not found\";
         else
         cout<<\"Number \"<<choice<<\" is found at \"<<index;
     }
     else
     cout<<\"Invalid choice\";
 
 
    return 0;
 }
Sample Output:
Elements in randomNums array
Before Sorting
76 21 447 126 26 313 114 274 107 474
After sorting
21 26 76 107 114 126 274 313 447 474
Elements in terminalSum array
Before Sorting
495 473 389 381 240
After sorting
495 473 389 381 240
Enter value between 0-500: 240
Number 240 is found at 4




