Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions:
• 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 calcAverage(), which uses the function to determine one of the five scores to drop.
Input Validation: Do not accept test scores lower than 0 or higher than 100.

#include <iostream>
#include <iomanip>
using namespace std;

void getScore (int,int,int,int,int);
void CalcAverage (int,int,int,int,int);
int FindLowest (int,int,int,int,int);

int main()
{
    int TestScore1, TestScore2, TestScore3, TestScore4, TestScore5;

    getScore (TestScore1,TestScore2,TestScore3,TestScore4,TestScore5);

    CalcAverage (TestScore1,TestScore2,TestScore3,TestScore4,TestScore5);
    return 0;
}
void getScore (int TestScore1,int TestScore2,int TestScore3,int TestScore4,int TestScore5)
{
    cout <<" Enter your first test score: ";
    cin >> TestScore1;
    cout <<" Enter your second test score: ";
    cin >> TestScore2;
    cout <<" Enter your third test score: ";
    cin >> TestScore3;
    cout <<" Enter your fourth test score: ";
    cin >> TestScore4;
    cout <<" Enter your fifth test score: ";
    cin >> TestScore5;


    while (TestScore < 0 || TestScore > 100) {
        cout <<" Please enter a valid score: ";
    cin >> TestScore;
    }
}
void CalcAverage (int Scr1,int Scr2,int Scr3,int Scr4,int Scr5);
{
    int sum , lowest;
    double avg;

    lowest = FindLowest ( Scr1,Scr2,Scr3,Scr4,Scr5);
    sum = Scr1 + Scr2 + Scr3 + Scr4 + Scr5 - lowest;
    avg = sum / 4.0;

    cout << setw(4) << fixed << showpoint << setprecision (2);
    cout <<" The average of the 4 highest scores are: "<< avg << endl;
}
int FindLowest (int Scr1,int Scr2,int Scr3,int Scr4,int Scr5)
{
    int lowest = Scr1;

    if (Scr2 < lowest) {
        lowest = Scr2;
    }
    else if (Scr3 < lowest) {
        lowest = Scr3;
    }
    else if (Scr4 < lowest) {
        lowest = Scr4;
    }
    else if (Scr5 < lowest) {
        lowest = Scr5;
    }
    cout <<" The lowest test score is: "<< lowest << endl;
    return lowest;
}

Recommended Answers

All 2 Replies

You should outline the problem with your code.

But your compiler should tell you you have no such variable as TestScore declared.

you have some problems specialy in getscore() function
and i don't see any advantage of line 32-45
since you're not using Array or Vectors you have to make a loop for each invalid score

i hope this helps you a little

#include <iostream>
#include <iomanip>
using namespace std;

void getScore(int &TestScore1, int &TestScore2, int &TestScore3, int &TestScore4, int &TestScore5);
void CalcAverage(int, int, int, int, int);
int FindLowest(int, int, int, int, int);

int main()
{
    int TestScore1 = 0, TestScore2 = 0, TestScore3 = 0, TestScore4 = 0, TestScore5 = 0; // first you have to assign values to 0

    getScore(TestScore1, TestScore2, TestScore3, TestScore4, TestScore5);

    CalcAverage(TestScore1, TestScore2, TestScore3, TestScore4, TestScore5);

    return 0;
}
void getScore(int &TestScore1, int &TestScore2, int &TestScore3, int &TestScore4, int &TestScore5) // you have to use reference or pointer  
                            //if you want to change a value inside a function                                                                       
{                                                                                                   

    cout << " Enter your first test score: ";
    cin >> TestScore1;
    while (TestScore1<0 || TestScore1>100){          //to test the score
        cout << " Ivalid score, try again" << endl;
        cout << " Enter your first test score: ";
        cin >> TestScore1;
    }
    cout << " Enter your second test score: ";
    cin >> TestScore2;
    while (TestScore2<0 || TestScore2>100){
        cout << " Ivalid score, try again" << endl;
        cout << " Enter your second test score: ";
        cin >> TestScore2;
    }
    cout << " Enter your third test score: ";
    cin >> TestScore3;
    while (TestScore3<0 || TestScore3>100){
        cout << " Ivalid score, try again" << endl;
        cout << " Enter your third test score: ";
        cin >> TestScore3;
    }

    cout << " Enter your fourth test score: ";
    cin >> TestScore4;

    while (TestScore4<0 || TestScore4>100){
        cout << " Ivalid score, try again" << endl;
        cout << " Enter your fourth test score: ";
        cin >> TestScore4;
    }
    cout << " Enter your fifth test score: ";
    cin >> TestScore5;

    while (TestScore5<0 || TestScore5>100){
        cout << " Ivalid score, try again" << endl;
        cout << " Enter your fifth test score: ";
        cin >> TestScore5;
    }
}
void CalcAverage(int Scr1, int Scr2, int Scr3, int Scr4, int Scr5)
{
    int sum, lowest;
    double avg;

    lowest = FindLowest(Scr1, Scr2, Scr3, Scr4, Scr5);
    cout << " Lowest score is " << lowest << endl;

    sum = Scr1 + Scr2 + Scr3 + Scr4 + Scr5-lowest;
    avg = sum / 4.0;

    cout << setw(4) << fixed << showpoint << setprecision(2);
    cout << " The average of the 4 highest scores are: " << avg << endl;
}
int FindLowest(int Scr1, int Scr2, int Scr3, int Scr4, int Scr5)
{
    int lowest = Scr1;

    if (Scr2 < lowest) {
        lowest = Scr2;
    }
    else if (Scr3 < lowest) {
        lowest = Scr3;
    }
    else if (Scr4 < lowest) {
        lowest = Scr4;
    }
    else if (Scr5 < lowest) {
        lowest = Scr5;
    }
    //cout << " The lowest test score is: " << lowest << endl;  // you don't need this here
    return lowest;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.