aftab2003
asked on
Struct and sort based on a field - how do I do that ?
I have the following code.
I need to find the highest scorer & dysplay that.
How do I include a sort system - perhaps a bubble sort to dysplay the info ?
My full code:
/*======================== =========* /
#include <iostream.h>
#include<stdio.h>
#define team 5
struct player_rec {
char name[15];
int score;
int rebounds;
int assists;
}stars[team]; /*declering a global structure type*/
void findmax(struct player_rec *pt); /*Function Prototype */
main()
{
struct player_rec stars[team]={ {"sam",10,6,2}, {"dan",12,9,1}, {"jim",2,10,0},{"james",0, 4,9}, {"nick",20,1,0} };
int i,maxscore;
for(i=0; i<team; i++)
/* ************************** ********** ********** ********** ******/
maxscore = stars[0].score; /* NOTE: IF i delete this stub of code, i get 5 player's record.*/
if (stars[i].score < maxscore) /* but i need the best scorer's record only.*/
stars[i].score = maxscore ; /* so i have to fix this portion of the code.*/
/************************* ********** ********** ********** ********/
findmax(stars + i);/* calling this function will traverse entire array
* and find best scorer*/
return;
};
void findmax(struct player_rec *pt) /* Function Header*/
{
printf("the maximum scorer is:%s\n",pt->name);
printf("the maximum score is:%d\n", pt->score);
printf("the maximum rebound is:%d\n",pt->rebounds);
return ;
}
I need to find the highest scorer & dysplay that.
How do I include a sort system - perhaps a bubble sort to dysplay the info ?
My full code:
/*========================
#include <iostream.h>
#include<stdio.h>
#define team 5
struct player_rec {
char name[15];
int score;
int rebounds;
int assists;
}stars[team]; /*declering a global structure type*/
void findmax(struct player_rec *pt); /*Function Prototype */
main()
{
struct player_rec stars[team]={ {"sam",10,6,2}, {"dan",12,9,1}, {"jim",2,10,0},{"james",0,
int i,maxscore;
for(i=0; i<team; i++)
/* **************************
maxscore = stars[0].score; /* NOTE: IF i delete this stub of code, i get 5 player's record.*/
if (stars[i].score < maxscore) /* but i need the best scorer's record only.*/
stars[i].score = maxscore ; /* so i have to fix this portion of the code.*/
/*************************
findmax(stars + i);/* calling this function will traverse entire array
* and find best scorer*/
return;
};
void findmax(struct player_rec *pt) /* Function Header*/
{
printf("the maximum scorer is:%s\n",pt->name);
printf("the maximum score is:%d\n", pt->score);
printf("the maximum rebound is:%d\n",pt->rebounds);
return ;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>I dont think jaime's code will funciton properly.
Have you tested it? Maybe you can explain us why.
Have you tested it? Maybe you can explain us why.
what was bugging me was:
maxscore = stars[0].score;
for(i=0; i<team; i++) {
if (stars[i].score >= maxscore)
themax = i;
}
in this loop, maxscore is never updated, so you will just find any player with more points than star 0.
maxscore = stars[0].score;
for(i=0; i<team; i++) {
if (stars[i].score >= maxscore)
themax = i;
}
in this loop, maxscore is never updated, so you will just find any player with more points than star 0.
int i,bestplayer; // index of player with the highest score
bestplayer = 0;
for(i=1; i<team; i++) {
if (stars[i].score > stars[bestplayer].score)
bestplayer = i;
}