calculating scores

I'm currently working on a program that calculate scores and show the ranks.
It works like this:

1) Input the 5 short forms of the names and their 5 scores.
E.g.
A 100 20 60 70 80
B 75 80 80 80 85
C 60 60 65 70 80
D 80 90 90 90 90
E 5 65 70 75 75

2) Delete each person's highest and lowest scores, then add up the remaining 3 scores.
E.g.
A 100(deleted) 80(add) 70(add) 60(add) 20(deleted)
Score of A=80+70+60=210

3) Rank the 1st, 2nd and 3rd. If two scores are the same, the person who got their scores entered 1st wins.
E.g.
First - D
Second - B
Third - A
**E's score is entered after A

My code is supposed to do these, but the input terminated when I entered 3 people and the output is also not correct. Please help me with it! Thanks a lot!
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
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
char name[5];
int score[5],totals[5]={0,0,0,0,0};
for(int i=0;i<=4;i++){
scanf("%c",&name[i]); //input the names
for(int j=0;j<=4;j++){
scanf("%d",&score[j]); //input each person's 5 scores
}
for(int m=0;m<3;m++){ // find out the 1st and last score
for(int k=0;k<=3;k++){ 
if(score[k]<score[k+1]) swap(score[k],score[k+1]); 
}
}
for(int l=1;l<=3;l++){
totals[i]+=score[l]; //sum up the remaining 3 scores
}
}
for(int j=0;j<3;j++){
for(int i=0;i<=3;i++){
if(totals[i]<totals[i+1]){
swap(totals[i],totals[i+1]); //sort the scores
swap(name[i],name[i+1]); //sort the names
}
}
}
printf("First - %c\n",name[0]); //output
printf("Second - %c\n",name[1]);
printf("Third - %c\n",name[2]);
return 0;
}
The format of printf and scanf. 'c' or 's'?
Should be c, because every name only consist of one letter
Line 8 should be scanf(" %c", &name[i]);. You want to skip whitespace prior to extracting a char.

If you actually wanted to use C++ input, the code might look something like:

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
#include<iostream>

int main()
{
    const unsigned nIds = 5;
    const unsigned nScores = 5;
    char id[nIds];
    unsigned scores[nScores];
    unsigned totals[nIds] = {};

    for (unsigned i = 0; i < nIds; ++i)
    {
        std::cin >> id[i];

        unsigned highest = 0;
        unsigned lowest = 0;

        for (unsigned j = 0; j < nScores; ++j)
        {
            std::cin >> scores[j];

            if (scores[j] > scores[highest])
                highest = j;
            if (scores[j] < scores[lowest])
                lowest = j;
        }

        for (unsigned j = 0; j < nScores; ++j)
            if (j != highest && j != lowest)
                totals[i] += scores[j];
    }

    const unsigned nPlaces = 3;
    unsigned places[nPlaces] = {};

    for (unsigned i = 0; i < nPlaces; ++i)
    {
        for (unsigned j = 0; j < nIds; ++j)
        {
            if (i > 0)    // sorting is for sissies!
            {
                bool shouldSkip = false;
                for (unsigned k = 0; k < i; ++k)
                    if (places[k] == j)
                        shouldSkip = true;

                if (shouldSkip)
                    continue;
            }

            if (totals[j] > totals[places[i]])
                places[i] = j;
        }
    }

    std::cout << "\nFirst - " << id[places[0]];
    std::cout << "\nSecond = " << id[places[1]];
    std::cout << "\nThird = " << id[places[2]] << '\n';

    return 0;
}
Last edited on
Umm... My new code literally worked but how can I sort the final result?
You could call std::sort() on your array of data.
Consider using a std::vector<Person>. std::sort can sort such array according to the Person::total.
1
2
3
4
5
class Person {
  char name;
  std::vector<unsigned int> scores;
  unsigned int total;
};

Remember though the initial assignment: "If two scores are the same, the person who got their scores entered 1st wins." Therefore, std::stable_sort is more to the point than std::sort.
Topic archived. No new replies allowed.