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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply