Arrays are passed by reference (address) by default.

Function takes an array and tries to return average, sum of elements, and the sum of the squares.

Calculate and display sum, sumsq using for loop in the function definition display sum, avg, and sumsq to see the difference between the values inside and outside function. If you see any difference explain why?

This is what I have:

``````void stats(double a[], int n, double & avg, double & sum, double & sumsq)
{
int i;
avg = 0.;
sum = 0.;
sumsq = 0.;
//how to calculate and display sum, sumsq using for loop in the function definition?
}

void main() {
double a[3] = {1, 3, 5};
double avg = 0, sum = 0, sumsq = 0;

stats(a, 3, avg, sum, sumsq);

//how to display sum, avg, and sumsq to see the difference between the values inside and outside function?

}``````
2
Contributors
1
2
Views
7 Years
Discussion Span
Last Post by VernonDozier

Line 7 - a[] and n are your inputs. How do you do you calculate average, sum, and sum squared on paper? Do it the exact same way here. You need a loop and the loop parameter is going to have something to do with n. Within that loop you're going to have to isolate individual elements of a[] and do something with them mathematically.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.