Write a program that generates an array of 10 elements filled up with random int
ID: 3633013 • Letter: W
Question
Write a program that generates an array of 10 elements filled up with random integer number rangingfrom 50 to 150, and display it on the screen.
After the creation and displaying of the array, the program displays the following:
[A]verage [E]ven [M]inimum Q[uit]
Please select an option:_
Then, if the user selects:
- A (lowercase or uppercase): the program displays the mean value of the numbers contained in the array
(2 decimal digits maximum).
- E (lowercase or uppercase): the program displays only the even numbers (not indexes) of the array.- M (lowercase or uppercase): the program displays the minimum number present in the array.
- Q (lowercase or uppercase): the program exits.
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
Explanation / Answer
Not sure what you mean at the end by "insert another integer"
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
static const int setSize=15;
static const int lowBound= 50;
static const int highBound=150;
int getnumber()
{
int rInt = rand() % (highBound-lowBound+1);
return rInt+lowBound;
}
void dumpvector(vector<int> & v)
{
for (vector<int>::iterator i = v.begin(); i < v.end(); i++)
cout << *i << " ";
cout <<endl;
}
int main()
{
vector<int> numbers;
vector<int> evens;
double mean = 0.0;
int smallest= 151;
bool done=false;
srand((unsigned)time(0));
for (int i = 0; i < setSize; i++)
{
int rInt = getnumber();
numbers.push_back(rInt);
if (rInt < smallest) smallest = rInt;
mean += rInt;
if ((rInt & 1) == 0) evens.push_back(rInt);
}
mean /= setSize;
sort(numbers.begin(),numbers.end());
sort(evens.begin(),evens.end());
cout << "Numbers selected" << endl;
dumpvector(numbers);
while (!done)
{
char selection;
cout << "Select Average, Even, Minimum, or Quit [A,E,M,Q]:";
cin >> selection;
selection &= ~0x20; // fold to upper case
if (selection == 'A')
cout << "Mean is " << mean << endl;
else if (selection == 'E')
{
cout << "Even elements only" << endl;
dumpvector(evens);
}
else if(selection == 'M')
cout << "Minimum is " << smallest << endl;
else if (selection == 'Q')
done = true;
else
cout << "invalid option" << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.