SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Arrays
Exercise:
Self-test Exercises
Question:17 | ISBN:9780321531346 | Edition: 7

Question

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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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

 

0 0

Discussions

Post the discussion to improve the above solution.