Please Write Code in C++ Write a program that generates an array filled up with
ID: 3712702 • Letter: P
Question
Please Write Code 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, [Ajverage, [Slearch, [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 (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 Clowercase 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
#include <iostream>
using namespace std;
int main()
{
int arr[10];
int i;
for(i=0;i<10;i++){
arr[i] = rand()%6+15;
}
while(true){
cout<<"[P]osition [R]everse [A]verage [S]earch [Q]uit"<<endl;
string ch;
cin>>ch;
if(ch=="P"||ch=="p"){
for(i=0;i<10;i++){
cout<<(i+1)<<" "<<arr[i]<<endl;
}
}
else if(ch=="R"||ch=="r"){
for(i=0;i<10;i++){
cout<<arr[9-i]<<endl;
}
}
else if(ch=="A"||ch=="a"){
float total = 0;
for(i=0;i<10;i++){
total += arr[i];
}
cout<<"Average "<<total/10<<endl;
}
else if(ch=="S"||ch=="s"){
cout<<"Enter an element to search"<<endl;
int s;
cin>>s;
int found=-1;
for(i=0;i<10;i++){
if(arr[i]==s){
found = i;
break;
}
}
if(found==-1){
cout<<"Element not found"<<endl;
}
else{
cout<<"Element found at "<<found<<endl;
}
}
else{
break;
}
}
return 0;
}
OUTPUT :
[P]osition [R]everse [A]verage [S]earch [Q]uit
P
1 20
2 20
3 19
4 19
5 20
6 19
7 15
8 15
9 19
10 17
[P]osition [R]everse [A]verage [S]earch [Q]uit
R
17
19
15
15
19
20
19
19
20
20
[P]osition [R]everse [A]verage [S]earch [Q]uit
A
Average 18.3
[P]osition [R]everse [A]verage [S]earch [Q]uit
S
Enter an element to search
19
Element found at 2
[P]osition [R]everse [A]verage [S]earch [Q]uit
S
Enter an element to search
16
Element not found
[P]osition [R]everse [A]verage [S]earch [Q]uit
Q
Process returned 0 (0x0) execution time : 32.030 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.