Write a program that generates an array filled up with random positive integer n
ID: 3544765 • Letter: W
Question
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 array.
- S (lowercase or uppercase): the program asks the user to insert an integer number and look for it
in the array, returning a message whether the number is found and its position (including multiple
occurrences, see example below), or is not found.
- E (lowercase or uppercase): the program exits.
NOTE: Until the last option (
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
using namespace std;
const int MAXSIZE = 20;
void reverse(int *arr)
{
cout << "Reverse of array: ";
for(int i = MAXSIZE-1 ; i >= 0; i--){
cout << arr[i] << " ";
}
cout << endl << endl;
}
int add(int *arr)
{
int sum = 0;
for(int i = 0; i < MAXSIZE; i++){
sum += arr[i];
}
return sum;
}
void search(int *arr, int target)
{
int counter = 0;//used to check if no value was found
for(int i = 0; i < MAXSIZE; i++){
if(arr[i] == target){
cout << "Target number found in position " << i << endl;
counter++;
}
}
if(counter == 0){
cout << "The target was not found in the array." << endl;
}
}
int main()
{
srand( (unsigned)time(NULL) );
//Did not indicate size of array or asked if user should give a size
int arr[MAXSIZE];
int target;
char choice;
for(int i = 0; i < MAXSIZE; i++){
//rand% 41 gives numbers from 0 to 40 adding 60 bumps it to 60 to 100
arr[i] = (rand()% 41) + 60;
}
//display array
cout << " Current array: ";
//while loop to repeat
while(1)
{
for(int i = 0; i < MAXSIZE; i++){
//rand%41 gives numbers from 0 to 40 adding 60 bumps it to 60 to 100
cout << arr[i] << " ";
}
cout << endl << endl;
//display options
cout << "[R]everse [A]dd [S]earch [E]xit ";
do{
cout << "Please select an option: ";
cin >> choice;
//convert to uppercase
choice = toupper(choice);
//check if exit call
if(choice == 'E'){
cout << "Exiting program...";
return 0;
}
//check if valid
if(choice != 'R' && choice != 'A' && choice != 'S'){
cout << "Invalid choice please try again. ";
}
}while(choice != 'R' && choice != 'A' && choice != 'S');
//switch statement to do operations
switch(choice){
case 'R':
reverse(arr);
break;
case 'A':
cout << "The sum of all elements in the array is: " << add(arr) << endl;
break;
case 'S':
cout << "Enter integer to search for: ";
cin >> target;
search(arr, target);
break;
default:
cout << "This should never be reached....";
break;
}
}
return 0; //really not needed
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.