Use arrays by reading a sequence of integers and then displaying them and their sum - C++ Data Type

C++ examples for Data Type:Array

Description

Use arrays by reading a sequence of integers and then displaying them and their sum

Demo Code

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int readArray(int integerArray[], int maxNumElements);
int sumArray(int integerArray[], int numElements);
void displayArray(int integerArray[], int numElements);

int main(int nNumberofArgs, char* pszArgs[])
{
    cout << "Terminate the loop by entering " << "a negative number\n";
    cout << endl;// www.j a  v a2s. com

    int inputValues[128];
    int numberOfValues = readArray(inputValues, 128);

    displayArray(inputValues, numberOfValues);
    cout << "The sum is " << sumArray(inputValues, numberOfValues) << endl;

    return 0;
}

int readArray(int integerArray[], int maxNumElements)
{
    int numberOfValues;
    for(numberOfValues = 0;numberOfValues < maxNumElements;numberOfValues++){
        int integerValue;
        cout << "Enter next number: ";
        cin  >> integerValue;

        if (integerValue < 0){
            break;
        }
        integerArray[numberOfValues] = integerValue;
    }
    return numberOfValues;
 }

void displayArray(int integerArray[], int numElements){
    cout << "The value of the array is:" << endl;
    for (int i = 0; i < numElements; i++){
        cout << i << ": " << integerArray[i] << endl;
    }
    cout << endl;
}

int sumArray(int integerArray[], int numElements){
    int accumulator = 0;
    for (int i = 0; i < numElements; i++){
        accumulator += integerArray[i];
    }
    return accumulator;
}

Result


Related Tutorials