Page 1 of 1

C++ The following program is supposed to get 5 non-negative integers (0 and up are valid) from the user, store them in a

Posted: Sun May 15, 2022 10:05 am
by answerhappygod
C++
The following program is supposed to get 5 non-negative integers
(0 and up are valid) from
the user, store them in an array, and calculate the sum of the
array elements. It has three
syntax errors and two logic errors. Type it in, fix the errors, and
make the following
modifications:
1. Print the array elements in row form.
2. Add a function to calculate the average of the array
elements.
3. Print the values: sum and average.
#include <iostream>

using namespace std;

const int SIZE=5

int totalVals(const int a[], int size);

int main()
{
int a[SIZE];
int i;

cout << "Enter " << SIZE << " non-negative
integers: ";

for (i = 0; i < SIZE; ++i)
{
a = -1;
while (a <= 0)
cin >> a;
}

cout << endl << "Sum = " << totalVals(a) <<
endl;
return 0;
}

int totalVals(const int a, int size)
{
int i, total=0;

for (i = 0; i < size; ++i);
total += a;

return total;
}

Example output (with input):

Enter 5 non-negative integers: 3 -2 0 5 -1 8 7

The valid values used are: 3 0 5 8 7

Sum = 23
Average = 4