Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that generates an array of 10 elements filled up with random int

ID: 3720760 • Letter: W

Question

Write a program that generates an array of 10 elements filled up with random integer number ranging from 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 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. uppercase): the program exits.

C++ program.

Explanation / Answer

Please find my implementation.


#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <ctime>

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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote