### getting an average of 3 numbers using functions

Okay. Everything in this works except for the calculation of the average.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````//calculate the average of three test scores //jett #include #include using std::endl; using std::cout; using std::cin; using std::setprecision; using std::fixed; double getTestScore(); double calcAverage(double, double); int main () { double score = getTestScore(); double sum = 0.0; for (int counter = 1; counter <= 3; counter ++) { cout << "Enter test score " << counter << ": "; cin >> score; } cout << fixed << setprecision(2); cout << "The average is " << calcAverage(score, sum) << endl; return 0; } //********Definitions************ double getTestScore() { double score = 0.0; double sum = 0.0; //accumulator sum += score; return score; } double calcAverage (double score, double sum) { double average = sum / 3; return average; }``````
I don't see how anything in the program could work. The calcAverage doesn't even use the score that is passed to it and assumes that the sum consists of 3 numbers added together which is a bad design. It should receive an array and size or two iterators (start and finish).

In the main the for loop overwrites the same variable 3 times so the first two entries are lost. I think that you want to setup an array of scores. The sum passed to calcAverage is zero because you never summed anything. getTestScore is pointless since it adds 0 + 0 and returns 0.

There are a lot of ways to redesign it. getTestScore could get 1 value and return it. Within it you could error check the value and loop until the user enters something valid. I'd start with that and get it working. In the main you call getTestScore for however many times you wish and add the return value into your array. Then pass the array to the average function and perform the calculation. I recommend passing pointers to the beginning and one past the end of the array and use the pointers like iterators summing the values and counting the number until you reach the end. Then divide the sum by the counter and return the average.
I haven't learned arrays yet. This chapter is only functions. I need to write this program using only loops and functions.

put your for loop in a seperate function returning a double (the score). make sure you save the score you read in to a variable (for example sum += score) every time you do a cin.
If it's always three numbers, rename your calcAverage function so you know it will always calculate the average of three numbers.
thank you joeriMJ, i didn't enter the accumulator, sum, into the loop. it works fine now.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````//calculate the average of three test scores //jett #include #include using std::endl; using std::cout; using std::cin; using std::setprecision; using std::fixed; double getTestScore(); double calcAverage(double, double); int main () { double score = getTestScore(); double sum = 0.0; for (int counter = 1; counter <= 3; counter ++) { cout << "Enter test score " << counter << ": "; cin >> score; sum += score; } cout << fixed << setprecision(2); cout << "The average is " << calcAverage(score, sum) << endl; return 0; } //********Definitions************ double getTestScore() { double score = 0.0; double sum = 0.0; //accumulator sum += score; return score; } double calcAverage (double score, double sum) { double average = sum / 3; return average; }``````