C++ program that calculates the average of three test scores
Posted by Samath
Last Updated: November 15, 2014

Create a C++ program that calculates the average of three test scores. The program should contain three value-returning functions: main(), getTestScore(), and calcAverage(). The main function should call the getTestScore function to get and return each of three test scores. The test scores may contain a decimal place. (Hint: The main function will need to call the getTestScore function three times.) The main function then should call the calcAverage function to calculate and return the average of the three test scores. When the calcAverage function has completed its task, the main function should display the average on the screen. Display the average with one decimal place.

Solution:

#include <iostream>
#include <iomanip>
using namespace std;
float getTestScore();
float calcAverage(float test1, float test2, float test3);

int main(int argc, char *argv[])
{
	float test1;
	float test2;
	float test3;
	float avg;
	
	test1 = getTestScore();
	test2 = getTestScore();
	test3 = getTestScore();
	avg = calcAverage(test1,test2,test3);
	cout <<setprecision(3)<<"Average: " << avg << endl;
	
	return 0;
}

float getTestScore()
{
	float score;
	
	cout<<"Enter test score: ";
	cin>>score;
	
	return score;
}

float calcAverage(float test1, float test2, float test3)
{
	float average = (test1+test2+test3)/3;
	return average;
}