Display object with highest/lowest xxx?

Can someone help me with my problem? I have difficult time with display the highest and lowest scorer in my program. Here is my sample program:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void dispFewest(Players player[], int size, char team_name[][ayat])
{
 cout << "\nYellow Card Details: " << endl;
 int fewest = player[0].yellowCards, count = 0;
 for(int i = 0; i < size; i++)
 {
  if(player[i].yellowCards < fewest){
   fewest = player[i].yellowCards;
   count = i;}
 else if(player[0].yellowCards < fewest){
   fewest = player[i].yellowCards;
   count = i;}}
  cout << "Name : " << player[count].name << endl;
  cout << "Team Name : " << team_name[count] << endl;
}


it run successfully when the first element has the highest yellowcards but when the first is the fewest, my compiler hang. Lol, i know. Please help me. Thx in advance!
Are you sure size > 0?

What are you trying to do with the else if part? To me it looks like you can just remove it because it will never run.
I finally figured it out. Here is the sample code:-

1
2
3
4
5
6
7
8
9
10
11
12
void dispFewest(Players player[], int size, char team_name[][ayat])
{
 cout << "\nYellow Card Details: " << endl;
 int fewest = player[0].yellowCards, count = 0;
 for(int i = 0; i < size; i++)
 {
  if(player[i].yellowCards <= fewest){
   fewest = player[i].yellowCards;
   count = i;}}
  cout << "Name : " << player[count].name << endl;
  cout << "Team Name : " << team_name[count] << endl;
}


Just a minor change...I don't know whether this code is working or not but it ran successfully for me. Thx for the reply tho!
Last edited on
Topic archived. No new replies allowed.