-1

I have to calculate the total of scores the participants received from the judges. And the one who gets the highest score will be the winner. I have to display the name of the winner but it displays the wrong winner. how do I solve this?

int main(){

int scores[5][2], i, j; int sum =0;
string name [5];

for(int i=0;i<5;i++)
{

    cout << "Enter participant's name: \n";
    cin >> name[i];
    cout<<"Enter scores for " << name[i] <<endl;
    for(j=0;j<2;j++)
    {
        cin >> scores[i][j];
    }
}


for(int i=0; i<5; i++)
{
    sum = 0;
    for(j=0; j<2; j++)
    {
        sum += scores[i][j];
    }
    cout<<"Total scores of " << name[i] << " " <<" is "<<sum<<endl;
}

int max, min, count;
int index[5];
max = min, count=0;
index[5]={0};
for(int i=0; i<5; i++)

    {
        for(j=0; j<2; j++)
        {
            if (scores[i][j]>max)
            {
                max=scores[i][j];
            }
        }
        for(int i=0; i<5; i++) 
        {
            if(scores[i][j]==max)
            {
                index[count]=scores[i][j];
                count++;
            }
        }
        if (count==1)
            cout<<"The winner is " << index[count-1] << name[i] << endl;
    }
    
    return 0;
    }
  • 1
    Your second `for` block at the end has `i` as iterable instead of `j`. That could be a source of errors – IWonderWhatThisAPIDoes Jan 29 '21 at 18:44
  • 2
    This is a very easy (and efficient) way to get it done. Why not store just the name and score of the highest scorer as soon as the input is given? Like store the highest score and the name of him somewhere, check with the newly entered score, if the new score is higher, store his name and score. Then later at the end, print it out. I'm not gonna give you the answer because its like doing your homework. It'll be a fun exercise! – D-RAJ Jan 29 '21 at 18:50
  • 1
    _@1004jih_ If you'd [debugged](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) your code stepping through line by line, that typo would probably have been easier to spot. – πάντα ῥεῖ Jan 29 '21 at 18:54
  • Is using an array a requirement? – Thomas Matthews Jan 29 '21 at 19:37
  • it's required to use 2d array for score and 1d array for participant's name – 1004jih Jan 30 '21 at 04:09

2 Answers2

0

You could use a running maxima algorithm without having to store all entries.

int main()
{
    std::string name;
    int         max_score = -1;
    std::string owner_max_score; 
    cout << "Enter participant's name: \n";
    while (std::getline(cin, name))
    {
        int score1, score2;
        cout<<"Enter 2 scores for " << name <<endl;
        cin >> score1 >> score2;
        if (score1 > max_score)
        {
            max_score = score1;
            owner_max_score = name;
        }
        if (score2 > max_score)
        {
            max_score = score2;
            owner_max_score = name;
        }
        cout << "\nEnter participant's name: \n";
    }
  cout << owner_max_score << " has highest score of " << max_score << endl;
  return 0;
}

To stop the loop, terminate the input. Some platforms use Ctrl-D, some use Ctrl-Z or Ctrl-C.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

You could also use a std::vector of structs:

struct Student
{
  std::string name;
  int         score1;
  int         score2;

  void console_input()
  {
    std::cout << "\n"
              << "Enter student's name:\n";
    std::getline(std::cout, name);
    std::cout << "\n"
              << "Enter 2 scores for "
              << name
              << "\n";
    std::cin >> score1 >> score2;
   }

   int max_score() const
   {
       return std::max(score1, score2);
   }
};

int main()
{
  Student s;
  std::vector<Student>  student_scores;
  std::cout << "Enter quantity of students: ";
  std::cint >> student_quantity;
  for (int i = 0; i < student_quantity; ++i)
  {
    s.console_input();
    student_scores.push_back(s);
  }
  std::string  owner_max_score = student_scores[0].name;
  std::int     highest_score = student_scores[0].max_score();
  for (int i = 1; i < student_quantity; ++i)
  {
    const int student_max_score = student_scores[i].max_score();
    if (student_max_score > highest_score)
    {
       highest_score = student_max_score;
       owner_max_score = student_scores[i].name;
    }
  }
  std::cout << "\n\n"
            << owner_max_score
            << " has highest score: "
            << highest_score  
            << "\n";
  return 0;
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154