Question #27706

write a program to input 5 integer numbers in an array. As each number is input, add the numbers to a total. After all numbers are input, display the numbers, their total, and their average.

Expert's answer

#include <iostream>

using namespace std;

void input_array(int* array, int arraySize)
{
cout << "Input an array of " << arraySize << " elements: ";
for (int c = 0; c < arraySize; c++)
cin >> array[c];
}

int calc_total(int* array, int arraySize)
{
int result = 0;
for (int c = 0; c < arraySize; c++)
result += array[c];
return result;
}

float calc_average(int* array, int arraySize)
{
return calc_total(array, arraySize) * 1.0 / arraySize;
}

void print_array(int* array, int arraySize)
{
cout << "Array elements:";
for (int c = 0; c < arraySize; c++)
cout << " " << array[c];
cout << endl;
}

int main()
{
int array[5];
input_array(array, 5);
print_array(array, 5);
cout << "The sum of array elements is: "
<< calc_total(array, 5) << endl;
cout << "The average value of array elements is: "
<< calc_average(array, 5) << endl;
return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS