Write a program that generates an array filled up with random positive integer n
ID: 3875363 • Letter: W
Question
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: Positioneverse, [A]verage,earch, [Q]uit 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 (lowercase 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 ('q'or'Q) is selected, the main program comes back at the beginning, asking the user to insert a new integer number.Explanation / Answer
C++ code:-
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int i,s[100],num,count, j=0,num1;
char ch;
float avg,sum,k;
for(i=1;i<=20;i++)
{
num = rand()%20;//As range upper bound bound is 20,we have taken 20,rand() is used to get random numbers
if(num >= 15 && num <= 20) //I am calculating all random numbers,but we want numbers between 15 and 20.
{
cout<<num<<" ";
s[j]=num; // I am inserting elements into array.
j++;
}
}
while(true) //rotating loop untill we quit.
{
cout<<" p or P for pair values r or R for reverse order a or A for average s or S for checking element q or Q for quiting.";
cout<<" "<<"enter choice:";
cin>>ch;
switch(ch)
{
case 'p' :
case 'P' : cout<<" "; //Here we can take P or P , so i have given same data for both.
for(i=0;i<j;i++)
cout<<"(s["<<i<<"],"<<s[i]<<") ";//I am printing as pair ie. (s[],value)
break;
case 'r' :
case 'R' :
cout<<" ";
for(i=j-1 ; i>=0 ; i--)//We have print in reverse order,so i started printing from last element
cout<<s[i]<<" ";
break;
case 'a' :
case 'A' : cout<<" ";
for(i=0;i<j;i++) // Here j is the last index of array.So we are looping upto j.
sum = sum + s[i]; //Adding all elements of array.
k = j ;
avg = sum / k ; //We are finding average of elements in array.
cout<<avg;
break;
case 's' :
case 'S' : cout<<" ";
cout<<"enter number to be searched:";
cin>>num1;
for(i=0;i<j;i++)
{
if(num1 == s[i])
{
cout<<num1<<","<<i;
count++;
}
}
if(count == 0)
cout<<"element not found";
break;
case 'q':
case 'Q' :
cout<<"thank you,exiting out."
exit(0) ;
break;
}
default:
cout<<"enter valid choice.";
}
}
Expected output:- here we loop untill user presses Q or q ,
your output will be like----
15 17 17 19
p or P for pair values r or R for reverse order a or A for average s or S for checking element q or Q for quiting.
enter choice: p
(s[0] , 15) (s[1] , 17) (s[2] , 17) (s[3] , 19)
p or P for pair values r or R for reverse order a or A for average s or S for checking element q or Q for quiting.
enter choice: R
19 17 17 15
p or P for pair values r or R for reverse order a or A for average s or S for checking element q or Q for quiting.
enter choice: a
17
p or P for pair values r or R for reverse order a or A for average s or S for checking element q or Q for quiting.
enter choice: S
enter number to be searched: 17
17,1 17,2
p or P for pair values r or R for reverse order a or A for average s or S for checking element q or Q for quiting.
enter choice: s
enter number to be searched: 24
element not found
p or P for pair values r or R for reverse order a or A for average s or S for checking element q or Q for quiting.
enter choice: t
enter valid choice.
p or P for pair values r or R for reverse order a or A for average s or S for checking element q or Q for quiting.
enter choice: q
thank you,exiting out.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.