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

IN C++ Write a program that generates an array filled up with random positive in

ID: 3718864 • Letter: I

Question

IN C++

Write a program that generates an array filled up with random positive integer number ranging from 15 to 20, and display it on the screen. After the creation and displaying of the array, the program displays the following: (Plosition Rjeverse, fAjverage, SJearch, [Qjuit Please select an option Then, if the user selects: P (lowercase or uppercase): the program displays the array elements position and value pairs for all member elements of the array. R (lowercase or uppercase): the program displays the reversed version of the array A (lowercase or uppercase): the program calculates and displays the average of the array elements -S Gowercase or uppercase): the program asks the user to insert an integer number and look for it in the array, returning the message wheher the number is found and its position including multiple occurences), or is not found. Q (lowercase or uppercase): the program quits NOTE: Until the last option(or Q) is selected, the main program comes back at the beginning, asking the user to insert a new integer nmber.

Explanation / Answer

Hi Friend,

Please find my implementation.

#include <iostream>
#include <stdlib.h>   
#include <time.h>
using namespace std;

int main()
{
   int array[10];
   srand (time(NULL));
   //create random integer in the range of 15 through 20
   //and store them in array
   int a=15;
int b=20;
for(int i=0;i<10;i++)
{
   array[i]=a + rand() % (b + 1 -a);
   }
   //display random numbers from array
   cout<<endl<<"Random numbers: "<<endl;
   for(int i=0;i<10;i++)
{
   cout<<array[i]<<" ";
   }
  
   int flag;
   int sum;
  
   while(1){
       //menu
   cout<<endl<<endl<<"Menu";
   cout<<endl<<"[P]osition [R]everse [A]verage [S]earch [Q]uit";
   //read user option
   cout<<endl<<"Please select an option: ";
   char option;
   cin>>option;
   //based on the option perform different actions
   switch(option)
   {
       //p or P : Display position and value pairs from array
       case 'P':
       case 'p':
           cout<<endl<<"Position and value pairs: "<<endl;
           for(int i=0;i<10;i++)
       {
           cout<<"("<<i<<","<<array[i]<<") ";
           }
           break;
       //r or R : Display array elements in reversed order
       case 'R':
       case 'r':
           cout<<endl<<"Reversed version of the array: "<<endl;
           for(int i=9;i>=0;i--)
       {
           cout<<array[i]<<" ";
           }
           break;  
       //a or A : calculate and display the average of all array elements
       case 'A':
       case 'a':
           sum=0;
           //find sum
           for(int i=0;i<10;i++)
       {
           sum+=array[i];
           }
           //display average
           cout<<endl<<"Average of array elements: "<<sum/10.0;
           break;
       //s or S : Search an integer
       case 'S':
       case 's':
           int num;
           //read integer from user
           cout<<endl<<"Enter an integer number to search: ";
           cin>>num;
           flag=0;
           //search entire array
           for(int i=0;i<10;i++)
       {
           //if found, display the position
           if(array[i]==num)
           {
               flag+=1;
               cout<<endl<<"Found at: "<<i;
               }
           }
       //not found case
           if(flag==0)
           {
               cout<<endl<<"Element not found";
           }
           break;
           //q or Q : Quit execution
       case 'q':
       case 'Q':
           exit(0);
   }
}
return 0;
}