Write a program that will read up to 10 nonnegative integers into an array
called number_array and then write the integers back to the screen. For
this exercise you need not use any functions. This is just a toy program
and can be very minimal.
C++ Program:
#include <iostream>
using namespace std;
//Define constant array size
const int SIZE = 10;
int main( )
{
//Declare variables
int number_array[SIZE];
int arrayIndex;
int posValue, indexVal = 0;
//Read up to 10 nonnegative integers into an array
//called number_array
cout << "Enter 10 non-negative(positive) integers:\n";
cin >> posValue;
while ( (posValue >= 0) && (indexVal < SIZE) )
{
number_array[indexVal] = posValue;
indexVal++;
cin >> posValue;
}
arrayIndex = indexVal;
//Display the integers back to the screen
cout << "Given array values are :";
for (indexVal = 0; indexVal < arrayIndex; indexVal++)
cout << number_array[indexVal] << " ";
cout<< endl;
return 0;
}
Output of the program code:
Enter 10 non-negative(positive) integers:
10
20
3
5
7
3
2
9
1
99
Given array values are :10 20 3 5 7 3 2 9 1 99