I was asked "This program asks for a series of five test scores and calculates the average score in a function. The function should receive the total of the five scores as an argument and return the average of the five scores. The program should not accept scores less than zero or greater than one hundred. The output should look like the example below."

Here's my code. It works, but I think I could do WAY better. Could anobody nudge me in the right direction? I really need to check each input as is comes in, but can't figure out a way to do that.

BTW I'm really green with C++ so be gentle. ;)

#include<iostream>
#include<string>


using namespace std;


int average(int a, int b, int c, int d, int e); // prototype average



int main()
{
  
	
	// declare variables
	
	int testscore = 0;
	int iaverage = 0;
	int f = 0;
	int g = 0;
	int h = 0;
	int i = 0;
	int j = 0;
	
	// get input
		
		cout << "Please enter 5 test scores ";
		cin >> f >> g >> h >> i >> j;

        if(f <= 0 || f > 100)
        {      
           cout << "Please enter a value between 0 - 100";
           cout << endl;       
        }         
        else
        {          
                   iaverage = average(f, g, h, i, j);
                   testscore = (f+g+h+i+j);
                   cout << "Total of all scores: " << testscore << endl;                  
                   cout << "Average score: " << iaverage << endl;
        }//end if-else
 
 
 return 0;
 
}//end main

int average(int a, int b, int c, int d, int e)
{
       int daverage;
       daverage = (a+b+c+d+e) / 5;
       return daverage;
} //end average

Do you know how to use arrays? If you are comfortable with arrays, try using them to replace the different mark variables.

Btw, this program doesn't check if the other variables apart from f, have been initialised to a value greater than 100 or lesser 0. Examine that you only are checking that for the variable 'f'.

the #include <string> is completely un-necessary!

Hello kadji.kahn,

I agree with everything that Sky Diploma said. In addition, you can shorten up your function a bit. This is what you currently have:

int average(int a, int b, int c, int d, int e)
{
       int daverage;
       daverage = (a+b+c+d+e) / 5;
       return daverage;
} //end average

I would first suggest that you make your return type a double, unless you are okay with rounding to the nearest whole number. You can get rid of the daverage variable and simplify the function to the following:

double average(int a, int b, int c, int d, int e)
{
     return (a+b+c+d+e) / 5.0;
}

If you decide to go the route that Sky Diploma suggested with arrays, the function wouldn't change drastically, you would just replace your arguements with an array of the correct size and replace a, b, c, d, and e with the appropriate array index.

I hope that helps a little.

-D

Comments
great input

Thanks for the quick reply!

I haven't go to arrays yet, so I can't do that.

Any idea on how to analyze each input w/o excessive "if" statements?

I appreciate the info on shorting up my function! Thats great, I see why the other code is excessive.

Ha! While its a nasty looking if statement that does work.

Cheers!

I appreciate the info.

Also, keep in mind that an if statement will only check the condition once (unless it is in a loop). If you have studied while loops, they may serve your purpose a little better as a while loop will allow the user to continue to input incorrect data until the data meets the specifications.

you're input was great, I was at least able to clean some things up, making the program a little better.

Have a great monday!

#include<iostream>

using namespace std;


int average(int a, int b, int c, int d, int e); // prototype average



int main()
{
  
	
	// declare variables
	
	int testscore = 0;
	int iaverage = 0;
	int f = 0;
	int g = 0;
	int h = 0;
	int i = 0;
	int j = 0;
	
	// get input
		
		

     while(cin)
	 {
		cout << "Please enter 5 test scores ";
		cin >> f >> g >> h >> i >> j;
		cout << endl;

		 if(f <= 0 || f > 100 || g <= 0 || g > 100 || h <= 0 || h > 100 || i <= 0 || i > 100 || j <= 0 || j > 100)
        {      
           cout << "Please enter a value between 0 - 100";
           cout << endl;       
        }         
        else
        {          
                   iaverage = average(f, g, h, i, j);
                   testscore = (f+g+h+i+j);
                   cout << "Total of all scores: " << testscore << endl;                  
                   cout << "Average score: " << iaverage << endl;
			    return 0;
        }//end if-else
	 }
 

 
}//end main

int average(int a, int b, int c, int d, int e)
{
       return (a+b+c+d+e) / 5.0;
} //end average

It's undersirable (in my opinion) to make the user re-enter all five scores just because ONE of them was wrong. Ask for each score individually using a for (or while) loop. You can also use "switch-case" statements in place of "if/else if" statements.

Some considerations when writing a program include: execution time (not so important when first learning to program; more important later)--fewer lines of code do not necessarily mean less execution time, user experience --how easy is the program to use, what are it's limitations (what user input could cause it to crash), what are it's features, readability and maintainability--how easy is it for someone else to read, understand, and modify your code (ex: use descriptive variable names: score1,score2, score3 instead of: a,b,c). I'm sure you can come up with more, but these are a few to get you started.

You might consider making all your variables of type "double" to allow for non-integer values. ex: 94.5

Edited 7 Years Ago by cgeier: n/a

This article has been dead for over six months. Start a new discussion instead.