| | |
Finding the lowest of five test scores
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I am writing the following program that calculates the average of a group of test scores where the lowest score in the group is dropped. It has the following functions:
But, as far as the void calcAverage() and the int findLowest() functions, I am totally confused. I have read my textbook. My instructor tells me that I can use an if-else statement for this program to find the lowest score; she doesn't want us using MAX or MIN or anything like that; we haven't gotten that far.
I need help!!!
- void getScore()-should ask the user for a test score, store it in a reference parameter variable, and validate it. This function should be called by main once for each of the five scores to be entered.
- void calcAverage() should calculate and display the average of the four highest scores. This function should be called just once by main and should be passed the five scores.
- int findLowest(): should find and return the lowest of the five scores passed to it. It should be called by the calcAverage, who uses the function to determine which of the five scores to drop.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <conio> using namespace std; //Function prototype void getscore(int, int, int, int, int); int main() { int score1, score2, score3, score4, score5; cout << "Enter your five test scores: "; cin >> score1 >> score2 >> score3 >> score4 >> score5; getscore (score1, score2, score3, score4, score5) <<endl; getch(); return 0; } void getscore(int score1, int score2, int score3, int score4, int score5) { cout << "Your five test scores are:\n"; cout << score1 <<endl; cout << score2 <<endl; cout << score3 <<endl; cout << score4 <<endl; cout << score5 <<endl; }
I need help!!!
If you feed a man a fish, you feed him for a day.
If you teach a man to fish, you feed him for a lifetime.
If you teach a man to fish, you feed him for a lifetime.
- Your implementation of getScore doesn't appear to be asking the user for anything....
- A no-brainer.
- Hint: stop using score1, score2, scorex and start using arrays. It will not only simplify the rest of your program, but it'll make searching for the smallest score as simple as creating a loop.
C++ Syntax (Toggle Plain Text)
getscore (score1, score2, score3, score4, score5) <<endl;
endl to something that's not a stream. "Technological progress is like an axe in the hands of a pathological criminal."
MIN and MAX won't really be of much value in your program anyway.
First use pencil & paper to find the minimum value of the 5 scores. That will give you a clue how to write the program. You would set the initial minimum value to be the value of the first number. Then look at the second number -- if it is less then the current minimum change to current minimum to be the second number. Do the same with each of the other 3 values. In your program you will have a series of 4 if statements, one if statement for each of the numbers 2 through 5.
Now do the same thing to find the maximum value.
[edit] I agree with Joe about using arrays, but only if you have learned them yet. If you can use arrays it will make the coding a lot easier[/edit]
First use pencil & paper to find the minimum value of the 5 scores. That will give you a clue how to write the program. You would set the initial minimum value to be the value of the first number. Then look at the second number -- if it is less then the current minimum change to current minimum to be the second number. Do the same with each of the other 3 values. In your program you will have a series of 4 if statements, one if statement for each of the numbers 2 through 5.
Now do the same thing to find the maximum value.
[edit] I agree with Joe about using arrays, but only if you have learned them yet. If you can use arrays it will make the coding a lot easier[/edit]
Last edited by Ancient Dragon; Apr 16th, 2007 at 12:48 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
There are a few problems with the existing code itself:
1. Requirement says "void getScore()-should ask the user for a test score, store it in a reference parameter variable,"
=> This is not done. So teh values updated within the function won't be reflected in main.
2. Usually it's a better idea to use an array of 5 elements instead of 5 int variables. So if you have already learned arrays you should use that.
Coming to the min/max part. Well you need to figure out which variable holds the min value. Try and write down a small algo in english to find this out.
Anyway before you come to this part I would say you tell us if you can use an array or not, things will depend on it.
1. Requirement says "void getScore()-should ask the user for a test score, store it in a reference parameter variable,"
=> This is not done. So teh values updated within the function won't be reflected in main.
2. Usually it's a better idea to use an array of 5 elements instead of 5 int variables. So if you have already learned arrays you should use that.
Coming to the min/max part. Well you need to figure out which variable holds the min value. Try and write down a small algo in english to find this out.
Anyway before you come to this part I would say you tell us if you can use an array or not, things will depend on it.
>Actually, we haven't even got to arrays in our textbook yet. So, I really don't know how to do them.
Sorry about that. I agree with AD and thekashyap, write out the algorithm with pencil and paper, and you'll see it much clearer.
Sorry about that. I agree with AD and thekashyap, write out the algorithm with pencil and paper, and you'll see it much clearer.
"Technological progress is like an axe in the hands of a pathological criminal."
actually, what AD says bout the max/min value is absolutely the best way to do this. You just have to assign the value in the first variable to another one,so you can compare with if´s, so if it is lower than the temporary variable, you assign the value you just compared to this varuable and continue comparing.
As for the getScore function, you should initiate the 5-scores variables outside of the main, so they are general to the whole program, so you ask the user for them inside the getScore() function.
Now, the calcAverage() function should not be a problem.
As for the getScore function, you should initiate the 5-scores variables outside of the main, so they are general to the whole program, so you ask the user for them inside the getScore() function.
Now, the calcAverage() function should not be a problem.
Well, I changed the getscore() function, and that is working fine.
However, I am confused on the findLowest function. This is what I came up with:
But, when I run it, it declares all of the scores to be the lowest. How do I find out (using if statements of if/else if statements) which one is the lowest?
However, I am confused on the findLowest function. This is what I came up with:
C++ Syntax (Toggle Plain Text)
void findLowest(int LO, int score1, int score2, int score3, int score4, int score5) { (LO = 1000); if (score1 < LO) { LO = score1; cout << "The lowest score is" <<LO <<endl; } else if (score2 < LO) { LO = score2; cout << "The lowest score is" <<LO <<endl; } else if (score3 < LO) { LO = score3; cout << "The lowest score is" <<LO <<endl; } else if (score4 < LO) { LO = score4; cout << "The lowest score is" <<LO <<endl; } else if (score5 < LO) { LO = score5; cout << "The lowest score is" <<LO <<endl; } }
But, when I run it, it declares all of the scores to be the lowest. How do I find out (using if statements of if/else if statements) which one is the lowest?
If you feed a man a fish, you feed him for a day.
If you teach a man to fish, you feed him for a lifetime.
If you teach a man to fish, you feed him for a lifetime.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Show Image in Form background using MSVC 6.0 in MFC application.
- Next Thread: linked list
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class code coding compile console conversion count data database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






