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

Write a program that generates an array filled up with random positive integers

ID: 3695249 • Letter: W

Question

Write a program that generates an array filled up with random positive integers with the number ranging from 60 to 100, and display it on the screen. After the creation and displaying of the array, the program displays the following: [R]everse [A]dd [S]earch E[xit] Please select an option:_

Then, if the user selects:

- R (lowercase or uppercase): the program displays the reversed version of the array.

- A (lowercase or uppercase): the program displays the sum of the elements of the array.

- S (lowercase or uppercase): the program asks the user to insert an integer number and look for it in the array, returning a message whether the number is found and its position (including multiple occurrences, see example below), or is not found.

- E (lowercase or uppercase): the program exits.

NOTE: Until the last option (‘e’ or ‘E’) is selected, the main program comes back at the beginning, asking the user to insert a new integer number.

Explanation / Answer

#include<iostream.h>
#include<conio.h>
char menu()
{
   char c;
   cout<<" Menu"<<endl;
   cout<<"[R]everse"<<endl;
   cout<<"[A]dd"<<endl;
   cout<<"[S]earch"<<endl;
   cout<<"[E]xit"<<endl;
   cout<<"Enter your choice";
   cin>>c;
   return c;
}

void rev(int a[])
{
   for(int i=4;i>=0;i--)
   {
       cout<<a[i]<<endl;
   }
}

void add(int a[])
{
   int s=0;
   for(int i=4;i>=0;i--)
   {
       s=s+a[i];
   }
   cout<<"Sum is "<<s;
}

void search(int a[],int n)
{
   int s=-1;
   for(int i=4;i>=0;i--)
   {
       if(a[i]==n)
       {
           s=i;
           cout<<"Position of number in array is "<<s;
           break;
       }

   }
   if(s==-1)
       cout<<"Number not found";


}

int main()
{
   int a[5],i,j;
   char c;
   cout<<"Enter 5 values in array";
   for(i=0;i<5;i++)
   {
       cin>>a[i];
   }

   cout<<"array values are:"<<endl;
   for(i=0;i<5;i++)
   {
       cout<<a[i]<<endl;
   }

   while(1)
   {
       c=menu();

       switch(c)
       {
           case 'r': case 'R':
               rev(a);
               break;

           case 'a': case 'A':
               add(a);
               break;

           case 's': case 'S':
               search(a,10);
               break;

           case 'e': case 'E':
               return 0;
               break;
       }

   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote