Calculate the average of a group of test scores, where the lowest score in the group is dropped

Trying to get this program to calculates the average of a group of test scores, where the lowest score in the group is dropped.

I've been on this program for 3 days now.

#include <iostream>
using namespace std;

int getScore();
float calcAverage();
int findLowest();
void displayOutput(int getScore, float calcAverage, int everage);

int main()
{
int test1, test2, test3, test4, test5;
float everageScore;

for (int i = 0; i < 5; i++)
{
test1 = getScore();
test2 = getScore();
test3 = getScore();
test4 = getScore();
test5 = getScore();

averageScore = ((test1 + test2 + test3 + test4 + test5) - lowest)
cout <<"Average Score is "<< averageScore <<endl;

getScore();
calcAverage();
findLowest();
displayOutput(int getScore,float calcAverage, int everage);

}
return 0;
}

int getScore()
{
for (int i = 0; i < 5; i++)
{
cout << "Please enter a test score. ";
cin >> getScore;
}
while (getScore < 0 || getScore > 100)
{
cout << "Valid test scores range from 0 - 100.\n";
cout << "Please enter a test score. ";
cin >> getScore;
}
}

float calcAverage()
{
calcAverage = (test1 + test2 + test3 + test4 + test5);
cout <<"Average Score is "<< calcAverage <<endl;
cout << "The average of the four highest test scores is "<< average<<endl;
}

int findLowest()
{
double lowest = test1;

if (lowest > test2)
lowest = test2;
if (lowest > test3)
lowest = test3;
if (lowest > test4)
lowest = test4;
if (lowest > test5)
lowest = test5;
}
void displayOutput(int getScore, float calcAverage, float everage)

{
//cout<<tScore<<endl;
//cout<<everage<<endl;
//cout<<getScore<<endl;
//cout<<lowest<<endl;
//cout<<score1<<endl;
//cout<<score2<<endl;
//cout<<score3<<endl;
//cout<<score4<<endl;
//cout<<score5<<endl;
}
Okay, so I think your teacher probably confused you about functions because you should only call functions in main when you want to use them.

1
2
3
4
getScore();
calcAverage();
findLowest();
displayOutput(int getScore,float calcAverage, int everage);


That should all disappear. Also you need to be passing test1 through test5 as parameters to the other functions to calculate the average and find the lowest value. If you don't pass them as arguments it doesn't know what their values are and is just going to generate a compiler error. Try and fix it up as much as you can and post again if you need more help.
Last edited on
//Please check this program because i think with this one i am heading to the right direction

#include <iostream>
using namespace std;

int getScore (int test1,int test2,int test3,int test4,int test5)
{
getScore = getScore(test1,test2,test3,test4,test5)
//getScore++;
for ( i = 0; i < 5; i++)
{
cout << "Please enter a test score. ";
cin >> getScore; //test1 >> test2 >> test3 >> test4 >> test5;
}
while (getScore < 0 || getScore > 100)

cout << "Valid test scores range from 0 - 100.\n";
cout << "Please enter a test score. ";
cin >> getScore; //test1 >> test2 >> test3 >> test4 >> test5;

}
void displayOutput(float averageScore)
{
averageScore = calcaverage(test1,test2,test3,test4,test5) /5;
cout<<averageScore<<endl;
}

float calcaverage(float test1, float test2, float test3, float test4, float test4)
{
return (test1 + test2 + test3 + test4, test4)/5;
}

int findLowest(int test1, int test2, int test3, int test4,int test5)
{
findLowest(test1,test2,test3,test4,test5)
}
int main()
{
int test1, test2, test3, test4, test5;
float averageScore;

for (int i = 0; i < 5; i++)
{
test1 = getScore();
test2 = getScore();
test3 = getScore();
test4 = getScore();
test5 = getScore();

//averageScore = averageScore (test1, test2, test3, test4, test5);

getScore(test1,test2,test,test4,test5);
averageScore = calcaverage(test1, test2, test3, test4, test5);

getScore(test1,test2,test,test4,test5);
findLowest = findLowest (test1,test2,test,test4,test5);

displayOutput(averageScore);
}
return 0;
}
Before you do anything more, you need to read up on functions. From your text book or on the web: for example http://www.cplusplus.com/doc/tutorial/functions/

Make sure that you can write one small program with one small function, and get it to compile and run without errors.

it seems like i am having an immense trouble with my functions...it's frustrates me...where do i need to correct.
think simple! I did this program in 30 sec!:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main()
{
int test[5], i;
double average=0.0;
for (i=0; i<5; i++)
{
    cin>>test[i];
}
sort (test, test+5);
for (i=1; i<5; i++)
{
    average+=test[i];
}
average/=4.0;
cout<<average;
system ("pause");
return 0;
}
> I did this program in 30 sec!:

So? What else is new?



> it seems like i am having an immense trouble with my functions...it's frustrates me...where do i need to correct.

Don't panic; take in one small step at a time and surely you will eventually reach where you want to be.

For the present, forget all about this: 'Calculate the average of a group of test scores, where the lowest score in the group is dropped'. Let's start with something much simpler.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std ;

int main()
{
    cout << "Please enter a number: " ;
    int i ;
    cin >> i ;

    int j = i ;
    cout << "you entered: " << j << '\n' ;
}


This program
a. accepts an integer entered by the user into the variable i
b. copies the value of i into another integer j
c. and finally prints out the value of j.

Now try to modify this by using a function int get_number() ; to accept an integer entered by the user.

This part of the code will be in the function get_number()
1
2
3
    cout << "Please enter a number: " ;
    int i ;
    cin >> i ;


And this part will be in main()
1
2
    int j = < the int returned by the function> ;
    cout << "you entered: " << j << '\n' ;


The skeleton for this program would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std ;

int get_number() ;

int main()
{
    int j = get_number() ; // j gets the value returned by the function
    cout << "you entered: " << j << '\n' ;
}

int get_number()
{
    // TODO: prompt the user for input
    // TODO: define an int variable that can hold the input
    // TODO: read the value from cin into the variable
    // TODO: return the value to the caller of the function (main) 
    // see: http://www.fredosaurus.com/notes-cpp/functions/return.html
}


Compile and run the program.

Once you have got this far, we can move on to the next (small) step.
Topic archived. No new replies allowed.