Display object with highest/lowest xxx?
Jan 11, 2012 at 12:48pm UTC
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!
Jan 11, 2012 at 12:56pm UTC
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.
Jan 11, 2012 at 1:03pm UTC
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 Jan 11, 2012 at 1:04pm UTC
Topic archived. No new replies allowed.