Well, your int n; is local to that block so it's going to disappear shortly thereafter. A lot of this depends on the specification of your problem. Can you use arrays? You don't have to, and it's more effective without them, but if that helps you understand then go for it. Write out an average of a set of numbers on a piece of paper and see how you do it, step by step.... i.e., add numbers up, divide by count of numbers, etc.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
you will need to declare an array of ints that hold the individual test scores so that they can be averaged later. Then pass that array to the function to computes the average. Declare and allocate the array between lines 11 and 12 of the code you posted. int* arry = new int[numValues];
Then delete line 15, and change line 16 to cin >> arry[count];
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Can you use arrays? You don't have to,
Since the average has to be done in a separate function I don't see any other way to do it but use an array. The number of scores is unknown until runtime so its not possible to just declare a bunch of integers.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
There are lots of tutorials on the net, here's just one of them.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I see your point Ancient. I guess I supposed that passing a running total and a number of scores into a function constructed thusly might be just as valid in this context.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
make the function averate() return the average so that main() can determine whether it is pass or fail.
int average(int n)
{
...
... <snip>
return sum/num;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Or another thing you can do is to call the grade function from Average ().
Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131