Lowest score drop

I need help with my assignment. I'm supposed to enter 5 test scores. Then, it is supposed to drop the lowest one. Then, it is supposed to average the other 4 test scores. I can't seem to drop the lowest test score. The average score is way off as well. Does anyone know what where my mistake is at?

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

int getScore(int test, int i, int size, int &total);
int findLowest(int myArray[], int size);
int calcAverage(int lowest, int total);

int main()
{
const int size = 5;
int myArray[size];
int test = 0;
int total = 0;
int lowest = 0;
int i = 0;

getScore(test, i, size, total);

findLowest(myArray, size);

calcAverage(lowest, total);

return 0;
}

int getScore(int test, int i, int size, int &total)
{
for (i = 0; i < size; i++)
{
cout << "Enter a test score:";
cin >> test;


while (test < 0 || test > 100)
{
cout << "INVALID! Try Again:";
cin >> test;
}
}
total += test;
return total;
}


int calcAverage(int lowest, int total)
{
int sum = 0;
int average = 0;

sum = total - lowest;
average = sum / 4.0;

cout << "The average score is:" << average << endl;
return average;
}

int findLowest(int myArray[], int size)
{
int lowest = myArray[0];
for (int i = 1; i < 5; i++)
{
if (myArray[i] < lowest)
lowest = myArray[i];
}

cout << "The lowest score is:" << lowest << endl;
return lowest;
}
First, Please use code tags. They help out alot.
http://www.cplusplus.com/articles/jEywvCM9/
Second, for some reason, your program isn't receiving your array integers correctly. I have made some edits here to show you:
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <iomanip>
#include <windows.h>
using namespace std;

int getScore(int test, int i, int &total);
void Score_Repeat(int myArray[]);
int findLowest(int myArray[]);
int calcAverage(int lowest, int total);

int main()
{
int myArray[5];
int test = 0;
int total = 0;
int lowest = 0;
int i = 0;

getScore(test, i, total);
Score_Repeat(myArray);
findLowest(myArray);

calcAverage(lowest, total);

return 0;
}

int getScore(int test, int i, int &total)
{
for (i = 0; i < 5; i++)
{
cout << "Enter a test score:";
cin >> test;


while (test < 0 || test > 100)
{
cout << "INVALID! Try Again:";
cin >> test;
}
}
total += test;
return total;
}

void Score_Repeat(int myArray[])
{
system("CLS");
cout<<"The Scores are:\n";
for (int i = 0; i <= 5; i++)
{
cout<<myArray[i]<<endl;
}
system("pause");
system("CLS");
}

int calcAverage(int lowest, int total)
{
int sum = 0;
int average = 0;

sum = total - lowest;
average = sum / 4.0;

cout << "The average score is:" << average << endl;
return average;
}

int findLowest(int myArray[])
{
int lowest = myArray[0];

for (int i = 1; i < 5; i++)
{
if (myArray[i] < lowest)
lowest = myArray[i];
}

cout << "The lowest score is:" << lowest << endl;
return lowest;
}
Last edited on
The problem is that you aren't storing the scores into the myArray. In getScore(), you read the scores into test and compute the total, but you don't store them in myArray.

I suggest the following:
1. Remove total from main(). There's no reason for it to be there. That means changing getScore and calcAverage so they don't take it as parameters.

2. Change getScore() to take myArray and size as the only parameters. Then change the code so it just populates myArray. That's all it should do.

3. Change calcAverage to take myArray, size and lowest as the parameters. calcAverage should compute total inside it.

> I'm supposed to enter 5 test scores. Then, it is supposed to drop the lowest one.
> Then, it is supposed to average the other 4 test scores.

We do not need an array to store the scores, do we?

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
26
27
28
29
30
31
32
33
34
#include <iostream>

int get_score( int min_score, int max_score )
{
    std::cout << "enter test score in [" << min_score << ',' << max_score << "]: " ;
    int score ;
    if( std::cin >> score && score >= min_score && score <= max_score ) return score ;

    std::cout << "invalid score. try again\n" ;
    std::cin.clear() ; // clear possible error state
    std::cin.ignore( 1000, '\n' ) ; // throw away the junk
    return get_score( min_score, max_score ) ; // try again
}

int main()
{
    const int NSCORES = 5 ;
    const int MIN_SCORE = 0 ;
    const int MAX_SCORE = 100 ;

    int total = 0 ;
    int lowest = MAX_SCORE ;

    for( int i = 0 ; i < NSCORES ; ++i )
    {
        const int score = get_score( MIN_SCORE, MAX_SCORE ) ;
        total += score ;
        if( score < lowest ) lowest = score ;
    }

    std::cout << "\nthe lowest score is " << lowest
              << "\naverage score after dropping the lowest score is "
              << ( total - lowest ) / double( NSCORES-1 ) << '\n' ;
}
@JLBorges We have to use arrays for this assignment.

@jasonwynn10 Thank you, I will try to start using code tags. I really appreciate your help!

@dhayden I am going to try this out, thank you so much! I really appreciate it!
Topic archived. No new replies allowed.