Brendon
Brendon

Reputation: 177

Erroneous calculation method in C++

I am taking a beginners C++ course and I am struggling with an assignment right now. The assignment was:

A particular talent competition has 5 judges, each of whom awards a score between 0 and 10 to each performer. Write a program that uses these rules to calculate and display a contestant’s score. It should include the following functions:

• int getJudgeData() should ask the user for a judge’s score, store it in a reference parameter variable, and validate it. This function should be called by main once for each of the 5 judges.

• double calcScore() should calculate and return the average of the 3 scores that remain after dropping the highest and lowest scores the performer received. This function should be called just once by main and should be passed the 5 scores.

Two additional functions, described below, should be called by calcScore, which uses the returned information to determine which of the scores to drop.

• int findLowest() should find and return the lowest of the 5 scores passed to it.

• int findHighest() should find and return the highest of the 5 scores passed to it.

When testing my program it works properly if the score from judge one is the lowest but it will now work properly for any other judges being the lowest.

Ex: I will enter 2,1,5,4,3 so it should drop the 1 & 5 and come out with the avg of 3 but the result is 2.6667

the code I have for int findLowest() is:

int findLowest(int scoreOne,int scoreTwo,int scoreThree,int scoreFour,int scoreFive)
{
    int lowest = scoreOne;
    if ( scoreTwo < lowest )
        lowest = scoreTwo;
    if ( scoreThree < lowest )
        lowest = scoreThree;
    if ( scoreFour < lowest )
        lowest = scoreFour;
    if ( scoreFive < lowest )
        lowest = scoreFive;

    return lowest;
}

The int findHighest is similar but the less than symbols are switched obviously.

for the calcAverage() function I have:

double calcAverage(double OneScore,double twoScore,double threeScore, double fourScore,double fiveScore)
{
    double lowest, highest, sum;

    lowest=findLowest(OneScore,twoScore,threeScore,fourScore,fiveScore);
    highest=findHighest(OneScore,twoScore,threeScore,fourScore,fiveScore);

    sum = (OneScore + twoScore + threeScore + fourScore + fiveScore);
    sum = sum - lowest;
    sum = sum - highest;
    sum = sum / 3;

    cout<<"\nAfter droping highest and lowest scores\n";
    cout<<"Your average score is "<<sum << endl;

return 0;
}

EDIT: I have put cout statements in the findHighest and findLowest functions to check what number it is determining is correct and each time it selects the correct highest number and for the lowest it will have 0

EDIT TWO: I have found that the program sets score one to 0 regardless of what is inputed. The program takes the correct input for the other scores.

Upvotes: 2

Views: 252

Answers (1)

Peter
Peter

Reputation: 508

After a quick look, the code you have should be working. Run through it again and make sure each if statement is consistent with what you want to do. If that doesn't work another way you could try is

double lowest = oneScore;

if (twoScore < lowest)
    lowest = twoScore;
if(threeScore < lowest)
    lowest = threeScore;

etc...

Your calcAverage() function is probably only failing because of the lowest() and hihgest() functions.

EDIT: If you have learned arrays, use it to declare an array of scores then use a for loop to iterate through, as Paul is hinting at in his comment

Upvotes: 1

Related Questions