hey, please don\'t copy from other chegg questions. make new unique code Write a
ID: 3875362 • Letter: H
Question
hey,
please don't copy from other chegg questions.
make new unique code
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, [Quit Please select an option: Then, if the user selects: -P (lowercase or uppercase): the program displays the array elements position and -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. Important notes, please read carefullys 1. On the due date please submit ONLY the Assignment2,cyour last names CPP file through blackboard. Late programs will not be evaluated and 0 points will be given. 2.Explanation / Answer
#include <iostream> //imports libraries.
#include <cstdlib>
#include <ctime>
using namespace std;
void displayMenu(){
int MAX_SIZE = 20, min_num = 0, max_num = 100; //initializing array size, random no max & min range.
int arr[MAX_SIZE], user_search, found = 0, message = 1; //initilizing and declaring variables.
char choice;
float average = 0;
srand(time(0)); //The srand() function sets the starting point for producing a series of pseudo-random integers.
for(int i=0; i<MAX_SIZE; i++){
arr[i] = rand() % (max_num + 1 - min_num) + min_num; //generating a random no. between max and min value(range).
cout << arr[i] << " "; //prints array.
}
while(true){ //while loop executes untill exit signal is recieved.
cout<<" **************************************** ";
cout<<"Enter a Choice: ";
cout<<"[P]osition, [R]everse, [A]verage, [S]earch, [Q]uit ";
cout<<"**************************************** ";
cin>>choice; //takes user choice input from the menu.
switch(choice){ //switch case for user choice of options.
case 'p':
case 'P': cout << "Index : Value ";
for (int i=0; i<MAX_SIZE; i++){ //traversing through array by using for loop.
cout << i << " : " << arr[i] << " ";
}
break;
case 'r':
case 'R': cout << "Reverse Array: ";
for (int i=(MAX_SIZE-1); i>-1; i--){
cout << arr[i] << " ";
}
break;
case 'a':
case 'A': for (int i=0; i<MAX_SIZE; i++){
average += arr[i];
}
cout << "Average : " << average / MAX_SIZE;
break;
case 's':
case 'S': cout << "Enter No. to be searched: ";
cin >> user_search;
for (int i=0; i<MAX_SIZE; i++){
if (user_search == arr[i]){
found = 1;
if (message == 1){
cout << "Number " << user_search << " Found. Locations : ";
message = 0;
}
cout << (i+1)<< " ";
}
}
if (found == 0){ //using found flag to keep track of no. found status.
cout << "Number Not Found. ";
}
cin.clear(); //incase the input by user is a non integer value, which will cause cin to go in failed state as cin is assigned to integer.
cin.ignore(50, ' '); //asks cin to discard 50 characters from the input stream.
break;
case 'q':
case 'Q': exit(0);
break;
default: cout<< "Invalid Input! ";
cin.clear();
cin.ignore(50, ' ');
break;
}
}
}
int main() //main function runs the program.
{
displayMenu(); //call to displayMenu function.
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.