| | |
Noob: inputing 5 grades and averaging them
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 12
Reputation:
Solved Threads: 0
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.
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.

C++ Syntax (Toggle Plain Text)
#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
0
#2 Oct 26th, 2009
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!
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!
1
#3 Oct 26th, 2009
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:
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:
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
I agree with everything that Sky Diploma said. In addition, you can shorten up your function a bit. This is what you currently have:
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
double average(int a, int b, int c, int d, int e) { return (a+b+c+d+e) / 5.0; }
I hope that helps a little.
-D
0
#6 Oct 26th, 2009
well you could use the logical && to join up checking for all !! and use one if statement only!
•
•
Join Date: Oct 2009
Posts: 12
Reputation:
Solved Threads: 0
0
#9 Oct 26th, 2009
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!
Have a great monday!
C++ Syntax (Toggle Plain Text)
#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
•
•
Join Date: Oct 2008
Posts: 103
Reputation:
Solved Threads: 14
0
#10 Oct 26th, 2009
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
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
Last edited by cgeier; Oct 26th, 2009 at 1:58 pm.
![]() |
Similar Threads
- calculate the grades for a student using if..else statements. (Java)
- I can call the function.. But not accurate anwer. (C++)
- Need Help Writing A Program (C++)
- Noob needs Wireless Network / router help. (Networking Hardware Configuration)
- This NOOB Needs Help (Networking Hardware Configuration)
- Noob question about Defining and Declaring. (C++)
- inputing system("date /t") and system("time /t") into a file (C++)
- percentage (C++)
- grades.java -- help!! (Java)
Other Threads in the C++ Forum
- Previous Thread: I don't know how to make this float function work correctly.
- Next Thread: getline
Views: 247 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





