did i do this right?

This is a discussion on did i do this right? within the C++ Programming forums, part of the General Programming Boards category; int findLowest() should find and return the lowest of the five scores passed to it. It should be called by ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    4

    did i do this right?

    int findLowest() should find and return the lowest of the five scores passed to it. It should be called by calcAverage, who uses the function to determine which of the five scores to drop.

    void calcAverage() should calculate and display the average of the four highest scores. This function should be called once just by main, and should be passed the five scores

    Code:
    #include <iostream>
    using namespace std;
    
    void getScore(int &);
    void calcAverage();
    int findLowest(int, int, int, int, int);
    
    int score1, score2, score3, score4, score5, lowest;
    
    int main()
    {
    	getScore(score1);
    	getScore(score2);
    	getScore(score3);
    	getScore(score4);
    	getScore(score5);
    	calcAverage();
    }
    
    void getScore(int &score)
    {
    	cout << "Test score? ";
    	cin >> score;
    	while (score < 0 || score > 100)
    	{
    		cout << "Test score cannot be lower than 0 or higher than 100.\n";
    		cout << "Test score? ";
    		cin >> score;
    	}
    }
    
    
    int findLowest(int score1, int score2, int score3, int score4, int score5)
    {
    	lowest = score1;
        if (score2 < lowest)
            lowest = score2;
    	if (score3 < lowest)
            lowest = score3;
    	if (score4 < lowest)
            lowest = score4;
    	if (score5 < lowest)
    		lowest = score5;
    	return lowest;
    }
    
    void calcAverage()
    {
    	findLowest(score1, score2, score3, score4, score5);
    	if (lowest == score1)
    		score1 = 0;
    	if (lowest == score2)
    		score2 = 0;
    	if (lowest == score3)
    		score3 = 0;
    	if (lowest == score4)
    		score4 = 0;
    	if (lowest == score5)
    		score5 = 0;
    	double average;
    	average = (static_cast<double>(score1) + score2 + score3 + score4 + score5) / 4;
    	cout << "The average of the four highest scores is " << average << endl;
    }
    thanks

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    it works, but I dont know if i did it the right way.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    it works, but I dont know if i did it the right way.
    how do you define "the right way"? if it works, shouldn't it be "the right way"?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    What if two scores are tied for the lowest? Your code will drop both. Consider using else if in a certain strategic place.

    I would not make the five score variables global, there is no need for that. Similarly, lowest should be local to the functions that use it.

    >> if it works, shouldn't it be "the right way"?
    No. Just because it works in your test cases doesn't mean it will always work.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    if two scores are the lowest, neither would drop, but I dont know how to fix it. is there a simpler way to find and drop the lowest score?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Usually arrays are used instead of a bunch of variables with a number attached to them.

    Without arrays: you need to hard-code all execution paths and you'll need enormous amounts of code. And if the requirements change - scores are added or removed - you'll need to rewrite everything.

    With arrays: you can devise and write algorithms that take less code and won't need to be rewritten if the array size changes..
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> if two scores are the lowest, neither would drop
    Really? I would think that it should drop only one score, that's usually how averages are done when dropping the lowest. If you really want to drop neither if two tie, then that would be hard to do, but I don't think that's what you want.

    >> is there a simpler way to find and drop the lowest score?
    I think what you've got is good. Just check my hint ("Consider using else if in a certain strategic place").

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If we assume the sequence of scores are:
    10, 12, 13, 14, 10

    The code submitted should find that 10 is the lowest score - I don't see how it WOULDN'T find that, so I don't understand the comment "if two scores are lowest, it won't find them".

    However this code will remove ALL instances that are equal to lowest:
    Code:
    void calcAverage()
    {
    	findLowest(score1, score2, score3, score4, score5);
    	if (lowest == score1)
    		score1 = 0;
    	if (lowest == score2)
    		score2 = 0;
    	if (lowest == score3)
    		score3 = 0;
    	if (lowest == score4)
    		score4 = 0;
    	if (lowest == score5)
    		score5 = 0;
    	double average;
    	average = (static_cast<double>(score1) + score2 + score3 + score4 + score5) / 4;
    	cout << "The average of the four highest scores is " << average << endl;
    }
    In the above example sequence, lowest = 10, and score1 and score5 are both 10, so both will be set to zero, which of course leads to an incorrect result (at least, if you want the FOUR highest scores to be averaged, rather than "all scores that aren't equal to the lowest").

    An easy solution would be to add an "else" after each score<n> is set to zero.

    Alternatively, use arrays, as suggested - and break the loop when you've found the first lowest value.

    Most C/C++ books have a chapter dedicated to arrays, and that is really part of learning how to program, so you should consider that IMPORTANT to learn. [It would make it REALLY PAINFULL if you had 1000 scores to remove both the lowest and highest from, right - because you'd have 4000 lines of code just to compare your 1000 variables and set one to zero.]

    --
    Mats

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    >> if two scores are the lowest, neither would drop
    Really? I would think that it should drop only one score, that's usually how averages are done when dropping the lowest. If you really want to drop neither if two tie, then that would be hard to do, but I don't think that's what you want.
    I agree there. If you don't drop one value, a person with two minimal scores might get a higher average than a person with one minimum, even if the second person has a higher total score.

    And your requirement would be too hard to program without arrays. Pressing Ctrl+V a lot of times can cause serious hand injuries
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21