Write a program that generates an array filled up with random positive integer n
ID: 3545726 • 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 (
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 ('e' or 'E') is selected, the main program comes back at the beginning, asking the user to insert a new integer number.Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main(){
srand(time(0));
int arr[10], i, sum;
bool found;
char choice;
for(i = 0; i < 10; i++){
arr[i] = rand() % 40 + 60;
}
cout << "The random generated array is: ";
for(i = 0; i < 10; i++){
cout << arr[i] << " ";
}
cout << " R[everse] [A]dd [S]earch E[xit] Select an option: ";
cin >> choice;
switch(choice){
case 'R':
case 'r':
cout << " The reversed array is: ";
for(i = 9; i >= 0; i--) cout << arr[i] << " ";
cout << endl << endl << "-----------------------------------------------------------------" << endl;
main();
case 'A':
case 'a':
cout << " The sum of the array elements is: ";
sum = 0;
for(i = 0; i < 10; i++) sum += arr[i];
cout << sum << " ";
cout << endl << endl << "-----------------------------------------------------------------" << endl;
main();
case 'S':
case 's':
cout << " Please insert an element to find: ";
int find;
found = false;
cin >> find;
for(i = 0; i < 10; i++){
if(arr[i] == find){
if(i == 0) cout << " The number " << find << " has been found at the 1st position" << endl;
else if(i == 1) cout << " The number " << find << " has been found at the 2nd position" << endl;
else if(i == 2) cout << " The number " << find << " has been found at the 3rd position" << endl;
else cout << " The number " << find << " has been found at the " << i + 1 << "-th position" << endl;
found = true;
}
}
if(!found) cout << " Element not found ";
cout << endl << endl << "-----------------------------------------------------------------" << endl;
main();
case 'E':
case 'e':
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.