Answer to Question #280926 in C++ for anaya

Question #280926

Write a program that inputs an array of 10 integers. After inputting display the values according to user choice and take a number from user. For example if user enters 7 then display first 7 elements of array.

1
Expert's answer
2021-12-17T18:18:09-0500
#include <iostream>


using namespace std;


const int NUMBERS_SIZE = 10;


int main() {
    int numbers[NUMBERS_SIZE];
    cout << "Enter " << NUMBERS_SIZE << " numbers:" << endl;


    for (int i = 0; i < NUMBERS_SIZE; i++) {
        cin >> numbers[i];
    }


    int howMany;
    while (1) {
        cout << "How many numbers to show:" << endl;
        cin >> howMany;


        if (howMany < 1 || howMany > NUMBERS_SIZE) {
            cout << "Number should be in range (1-" << NUMBERS_SIZE << "), try again..." << endl;
            continue;
        }


        break;
    }


    cout << "First " << howMany << " numbers:" << endl;
    for (int i = 0; i < howMany; i++) {
        cout << numbers[i] << " ";
    }
    cout << endl;


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog