Question

Write a C++ program that generates an array filled up with random positive integer number ranging...

Write a C++ 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:

[P]osition [R]everse, [A]verage, [S]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.

1 0
Add a comment Improve this question Transcribed image text
Answer #1
  • Firstly, create a random array using the rand() function and map it to a value between 15 and 20 by calculating : 15 + rand() % 5
  • then, display the menu to the user. For each operation, we will pre define a fucntion to perform those tasks. In this program we will have these functions:
    • printArray(): to print array along with their position
    • printReverse(): to print the array in reverse
    • printAverage(): to print the average of array
    • searchArray(): to search for an element in array
  • based on the user's choice these methods will be invoked. if the user enters 'Q" or 'q', the program terminates.

program:

#include <iostream>

using namespace std;

void printArray(int* a){
for(int i = 0; i<5; i++){
cout<<"\n";
cout<<"element at position "<<i+1<<": "<<a[i];
}
}

void printAverage(int* a){
float average = 0;
for(int i = 0; i<5; i++)
average += a[i];
average = (float)average/5;
cout<<"\n average of array elements = "<<average<<"\n";
}

void printReverse(int* a){
cout<<"\n";
for(int i = 4; i>=0; i--)
cout<<a[i]<<" ";
cout<<"\n";
}

void searchArray(int* a, int target){
int i;
for( i = 0; i<5; i++){
if(a[i]==target){
cout<<"Element found at index "<<i<<"\n";
break;
}
}
if(i==5)
cout<<"Element not found<<\n";
}
int main(){
int randArray[5];
for(int i = 0; i<5;i++){
randArray[i] = 15 + rand()%5;
}
char choice;
do{
cout<<"[P]osition\n";
cout<<"[R]everse\n";
cout<<"[A]verage\n";
cout<<"[S]earch\n";
cout<<"[Q]uit\n";
cout<<"Your choice: ";
cin>>choice;
switch(toupper(choice)){
case 'P':
printArray(randArray);
break;
case 'R':
printReverse(randArray);
break;
case 'A':
printAverage(randArray);
break;
case 'S':
int target;
cout<<"\nElement to be searched: ";
cin>>target;
searchArray(randArray, target);
break;
  
}
cout<<"\n";
}while(toupper(choice)!='Q');
}

console inputs and outputs:

[P]osition [R]everse [A]verage [S]earch [Q]uit Your choice: P element at position 1: 18 element at position 2: 16 element atElement to be searched: 17 Element found at index 2 [P]osition [R]everse [A]verage [S]earch [Q]uit Your choice: a

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that generates an array filled up with random positive integer number ranging...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that generates an array filled up with random positive integer number ranging from...

    Write a program that generates an array filled up with random positive integer 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...

  • in C++ please ELET 2300 Programming Assignment # 2 Write a program that generates an array...

    in C++ please ELET 2300 Programming Assignment # 2 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: [P]osition [Reverse, [A]verage, (Search, [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...

  • Please provide the full and correct solution to this problem. Make sure to provide comments so I can understand what is going on. Correct solution will receive thumbs up. Thanks. Write a program that...

    Please provide the full and correct solution to this problem. Make sure to provide comments so I can understand what is going on. Correct solution will receive thumbs up. Thanks. 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, [SJearch, [Qjuit Please select an option: Then, if the...

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • 1. Write a C code that do the following: Generate array of random integer numbers of...

    1. Write a C code that do the following: Generate array of random integer numbers of size 25 Fill the array with random integers ranging from [1-100] Find the maximum number of the array and its location Find the minimum number of the array and its location Search for a number in the array Print the array Flip the array Sort this array Quit the program when the user choose to exit

  • Q#1 Write a C++ program that reads n integer values and stores them in an array...

    Q#1 Write a C++ program that reads n integer values and stores them in an array of maximum 20 integers. Read from the user a valid value of n (n should not exceed 20). After reading the n array elements find the maximum and minimum elements.

  • 2. Write a program that reads N integer numbers of array from the user and then...

    2. Write a program that reads N integer numbers of array from the user and then displays the sum, max number and the numbers of the array use four subroutines ( Read_Array(), Calculate_Sum(), Find_Max() and Display_Array()). Here is sample output Please enter number of elements: 4 Please enter the required numbers: 50 100 150 20 The entered numbers are: 50 100 150 20 Sum is : 320 Max is 150 by Mpsi ,sum and max to call subroutine and retrieve...

  • Write a C program that does the following: • Displays a menu (similar to what you...

    Write a C program that does the following: • Displays a menu (similar to what you see in a Bank ATM machine) that prompts the user to enter a single character S or D or Q and then prints a shape of Square, Diamond (with selected height and selected symbol), or Quits if user entered Q. Apart from these 2 other shapes, add a new shape of your choice for any related character. • Program then prompts the user to...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • 2. Write a C++ program, that generates a set of random integers and places them in...

    2. Write a C++ program, that generates a set of random integers and places them in an array. The number of values generated and their range are entered by the user. The program then continually prompts the user for one of the following character commands: a: display all the values I: display the values less than a given value g display the values greater than a given value e: display all odd values o: display all even values q: quit...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT