Solved

Struct and sort based on a field - how do I do that ?

Posted on 2004-09-25
4
203 Views
Last Modified: 2010-04-15
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 ;
}

0
Comment
Question by:aftab2003
  • 2
  • 2
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
ID: 12152474
I think you need some retouches. There are many alternatives, here is simple one:

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,themax;
maxscore = stars[0].score;

for(i=0; i<team; i++) {
      if (stars[i].score >= maxscore)
            themax = i;
}

findmax(stars + themax);
};

0
 
LVL 1

Expert Comment

by:Feldspar
ID: 12152922
I dont think jaime's code will funciton properly.  I suggest:

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;
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12152975
>I dont think jaime's code will funciton properly.
Have you tested it? Maybe you can explain us why.
0
 
LVL 1

Expert Comment

by:Feldspar
ID: 12156413
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.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I recover data from my iphone 3g 4 54
chcp 65001 File encoding 66 230
Passing a array as parameter - C 2 84
Resolve Dependency Issues 4 47
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now