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.
#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;
}
Comments
Leave a comment