You are using a version without Ads of this website. Please, consider donating:

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.
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!
 ``123456789101112131415161718192021222324252627282930313233`` ``````#include #include 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]
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:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include 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.
 ``12345`` ``````class Person { char name; std::vector 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.

You are using a version without Ads of this website. Please, consider donating: