954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Computing Average Test Scores

Hi guys. I am trying to write a program that asks the user how many test scores they have, then reads the test scores, averages them, then displays the average and a pass/fail grade (50+ is a pass), using a seperate function to average the input values.

I have been working on it for about 3 hours now, and I don't even know if I am going in the right direction. Worse, I don't know how to proceed. I have the program asking for the number of test scores, and reading the test scores. I have no idea how to write the function to average them.

Any help would be greaty appreciated:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	cout << "This program computes the average of a set of test scores."<<endl;
	cout << "How many test scores would you like to average? ";
	int numValues;
	cin >> numValues;
	for (int count = 1; count <= numValues; count++)
	{
		cout << "\nEnter the test score: ";
		int n;
		cin >> n;
	}
}
cole davidson
Newbie Poster
20 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
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
Team Colleague
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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thanks! I think I understand what you guys are saying, however we have not learned arrays yet. Are there no other ways? Or if there aren't, could you explain how to use arrays?

cole davidson
Newbie Poster
20 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

There are lots of tutorials on the net, here's just one of them.

Ancient Dragon
Retired & Loving It
Team Colleague
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
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

Ok. I have managed to figure it out. I have the program working as far as returning the average, but now I am having trouble getting it to display the pass or fail. I think I have written the function correctly for it (please correct me if I'm wrong), but the stupid thing is I can't the main function to display it! Agh! It frustrates me so much sometimes!

Anyways, here's the code. How do I get the main function to display the result of the pass or fail function (string grade), if that function is even correct!?

#include <iostream>
#include <string>
using namespace std;

string grade(double sum, int num);
void average (int num)
{
       double sum =0;
       double score;
for (int count = 1; count <= num; count++)

{

cout <<"\nEnter the test score: ";

cin >>score;
sum =sum +score;
}
cout <<"The average is " <<sum/num<<endl;
}



int main()
{

cout <<"This program computes the average of a set of test scores."<<endl;
cout <<"How many test scores would you like to average? ";
int numValues;
cin >> numValues;

average (numValues);


}
string grade(double sum, int num)
{
	if (sum/num >= 50)
		return "This student has passed!";
	else if (sum/num < 50)
		return "This student has failed.";
}
cole davidson
Newbie Poster
20 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
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
 

Ok. But is the grade function correct?

cole davidson
Newbie Poster
20 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Ahha! Yes! It works! Thank you so much for your help!

cole davidson
Newbie Poster
20 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You