40,421 views
21 votes
21 votes
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 [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.

User Louise K
by
3.0k points

1 Answer

24 votes
24 votes

Answer:

#include <iostream>

#include <cstdlib>

using namespace std;

const int MIN_VALUE = 15;

const int MAX_VALUE = 20;

void fillArray(int arr[], int n) {

// fill up the array with random values

for (int i = 0; i < n; i++) {

arr[i] = rand() % (MAX_VALUE - MIN_VALUE + 1) + MIN_VALUE;

}

}

void displayArray(int arr[], int n) {

for (int i = 0; i < n; i++) {

cout << i << ": " << arr[i] << endl;

}

}

void main()

{

int myArray[10];

int nrElements = sizeof(myArray) / sizeof(myArray[0]);

fillArray(myArray, nrElements);

displayArray(myArray, nrElements);

char choice;

do {

cout << "[P]osition [R]everse, [A]verage, [S]earch, [Q]uit ";

cin >> choice;

choice = toupper(choice);

switch (choice) {

case 'P':

displayArray(myArray, nrElements);

break;

}

} while (choice != 'Q');

}

Step-by-step explanation:

Here's a start. Now can you do the rest? ;-)

User Britztopher
by
3.3k points