getting an average of 3 numbers using functions

Okay. Everything in this works except for the calculation of the average.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//calculate the average of three test scores
//jett

#include <iostream>
#include <iomanip>

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//calculate the average of three test scores
//jett

#include <iostream>
#include <iomanip>

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;
}
still, getTestScore() does not add any value to your program except initialize your score variable
Topic archived. No new replies allowed.