Finding the lowest of five test scores

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

Finding the lowest of five test scores

 
0
  #1
Apr 16th, 2007
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:
  1. 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.
  2. 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.
  3. 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.
So far, here is my version of the program:
  1. #include <iostream>
  2. #include <conio>
  3. using namespace std;
  4. //Function prototype
  5. void getscore(int, int, int, int, int);
  6.  
  7. int main()
  8. {
  9. int score1, score2, score3, score4, score5;
  10. cout << "Enter your five test scores: ";
  11. cin >> score1 >> score2 >> score3 >> score4 >> score5;
  12. getscore (score1, score2, score3, score4, score5) <<endl;
  13. getch();
  14. return 0;
  15. }
  16. void getscore(int score1, int score2, int score3, int score4, int score5)
  17. {
  18. cout << "Your five test scores are:\n";
  19. cout << score1 <<endl;
  20. cout << score2 <<endl;
  21. cout << score3 <<endl;
  22. cout << score4 <<endl;
  23. cout << score5 <<endl;
  24. }
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!!!
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Finding the lowest of five test scores

 
0
  #2
Apr 16th, 2007
  1. Your implementation of getScore doesn't appear to be asking the user for anything....
  2. A no-brainer.
  3. 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.
BTW, this is wrong:
  1. getscore (score1, score2, score3, score4, score5) <<endl;
Can't add endl to something that's not a stream.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Finding the lowest of five test scores

 
0
  #3
Apr 16th, 2007
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]
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Finding the lowest of five test scores

 
0
  #4
Apr 16th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

Re: Finding the lowest of five test scores

 
0
  #5
Apr 16th, 2007
Actually, we haven't even got to arrays in our textbook yet. So, I really don't know how to do them.
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Finding the lowest of five test scores

 
0
  #6
Apr 16th, 2007
>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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

Re: Finding the lowest of five test scores

 
0
  #7
Apr 16th, 2007
Ok, I'll try that and come back.

Thanks for all of your help.

:-)I'll make sure to give you guys credit when I complete this program!!!
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Finding the lowest of five test scores

 
0
  #8
Apr 16th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

Re: Finding the lowest of five test scores

 
0
  #9
Apr 20th, 2007
Got it! Thanks
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

Re: Finding the lowest of five test scores

 
0
  #10
Apr 22nd, 2007
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:

  1.  
  2. void findLowest(int LO, int score1, int score2, int score3, int score4, int score5)
  3. {
  4. (LO = 1000);
  5. if (score1 < LO)
  6. {
  7. LO = score1;
  8. cout << "The lowest score is" <<LO <<endl;
  9. }
  10. else if (score2 < LO)
  11. {
  12. LO = score2;
  13. cout << "The lowest score is" <<LO <<endl;
  14. }
  15. else if (score3 < LO)
  16. {
  17. LO = score3;
  18. cout << "The lowest score is" <<LO <<endl;
  19. }
  20. else if (score4 < LO)
  21. {
  22. LO = score4;
  23. cout << "The lowest score is" <<LO <<endl;
  24. }
  25. else if (score5 < LO)
  26. {
  27. LO = score5;
  28. cout << "The lowest score is" <<LO <<endl;
  29. }
  30. }

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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC