• C

Searching an array for match, and bringing back associated subscript.

I need to prompt the user for a golfer id, search the array for the golfer id, and return the subscript of the location in the array.  How do I do this in main, and how do I write my function?  I'm trying to report golf scores.  The search consists of fields golfer id, golfer name, average score,  rating.  The report format looks like this:

golfer id   golfer name     avg score         rating
   52              smith                52                  a
   36               jones               41                  b

please help. I need the way to declare, to call in main, and the actual function to do this.

Thanks for you help.
 
zaya69Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RONSLOWCommented:
use bsearch
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RONSLOWCommented:
or just write a simple loop .. eg

struct Golfer {
  int id;
  char* name;
  int avg;
  char rating;
};

#define MAXGOLFER 100

int main() {
  struct Golfer golfer[MAXGOLFER] = {
    52,"smith",52,'a',
    36,"jones",41,'b'
  };
  int ngolfers = 2;
  int idtofind = 36;
  int foundat = -1;
  for (int i = 0; i < ngolfers; i++) {
    if (golfer[i].id == idtofind) {
      foundat = i;
      break;
    }
  }
  printf("found id:%d, name:%s, avg score:%d, rating:%c\n",
  golfer[foundat].id,golfer[foundat].name,
  golfer[foundat].avg,golfer[foundat].rating);
  return 0;
}

0
RONSLOWCommented:
BTW: last comment should have ended with
  if (foundat >= 0) {
    printf ...
  } else {
    printf("not found\n");
  }
  return 0;
}
also I forgot I was using C and not C++ .. so you'll need to declare 'int i;' at the start of main, not where I put it inside the for statment (that is C++ only)

Using bsearch you need to ensure the array is sorted first (with qsort).  Also bsearch returns a pointer to the item found, rather than a subscript into the array.

struct Golfer {
  int id;
  char* name;
  int avg;
  char rating;
};

#define MAXGOLFER 100

static int compare_ids (const Golfer* p1, const Golfer* p2) {
  if (p1->id < p2->id) return -1;
  if (p1->id > p2->id) return +1;
  return 0;
}

int main() {
  /* the golfers */
  struct Golfer golfer[MAXGOLFER] = {
    52,"smith",52,'a',
    36,"jones",41,'b'
  };
  int ngolfers = 2;
  /* the golfer to find */
  struct Golfer find = {0};
  find.id = 36;
  /* the golfer we found */
  golfer* pfound = NULL;
  /* sort the array of golfers */
  qsort(golfer,ngolfer,sizeof(Golfer),
    (int(*)(const void*,const void*))compare_ids);
  /* search the array of golfers */
  pfound = bsearch(find, golfer,ngolfer,sizeof(Golfer),
    (int(*)(const void*,const void*))compare_ids);
  /* if found, report it */
  if (pfound) {
    printf("found id:%d, name:%s, avg score:%d, rating:%c\n",
    pfound->id,pfound->name,
    pfound->avg,pfound->rating);
  } else {
    printf("not found\n");
  }
  return 0;
}

0
Top Threats of Q1 & How to Defend Against Them

WEBINAR: Join WatchGuard CTO and our Threat Research Team on Aug. 2nd to hear the findings from our Q1 Internet Security Report! Learn more about the top threats detected in the first quarter and how you can defend your business against them!

zaya69Author Commented:
Ronzlow:

I must apologize, but I have no idea what you're talking about.  You're way over my head.  Let me rephrase my question.  Here's what I need answered (in order):

1. prompt user for golfer id
2. search the id array for match
3. if match found, return the location in the array
4. use the location to report all info on golfer
5. if no match, then display an error message (ie: no such golfer)

I have no idea what bsearch is, and I'm not even up to pointers yet.  So, based on that information, how do I go about coding this properly?

Thanks for your assistance!
0
RONSLOWCommented:
That is what my first sample does. .. except it doesn't prompt for the golfer id, it was just hard coded to search for a particular one.

here is is again (with some extra comments)...

/* the info required for a golfer */
struct Golfer {
  int id;  /* golfer id */
  char* name;  /* golfers name */
  int avg;  /* average score */
  char rating;  /* rating */
};

/* max number of golfers we will handle */
#define MAXGOLFER 100

int main() {
  /* here are the two golfers you put in your question */
  struct Golfer golfer[MAXGOLFER] = {
    { 52,"smith",52,'a' },
    { 36,"jones",41,'b' }
  };
  /* there are two golfers in the array */
  int ngolfers = 2;
  /* I want to find golfer with id==36 */
  int idtofind = 36;
  /* init the array subscript to -1 until we find something */
  int foundat = -1;
  /* loop for each golfer in the array */
  for (int i = 0; i < ngolfers; i++) {
    /* if this is the golfer we want */
    if (golfer[i].id == idtofind) {
      /* remember the subscript */
      foundat = i;
      /* and stop looking */
      break;
    }
  }
  /* if we found the golfer */
  if (foundat >= 0) {
    /* print the info */
    printf("found id:%d, name:%s, avg score:%d, rating:%c\n",
      golfer[foundat].id,golfer[foundat].name,
      golfer[foundat].avg,golfer[foundat].rating);
  } else {
    /* otherwise say we couldn't find */
    printf("not found\n");
  }
  /* that's all folks */
  return 0;
}

You can enhance by adding some lines to prompt for the id

  printf("Enter golfer id to find: ");
  scanf ("%d",&idtofind);

I'm sure you can work out where to put them

BTW: bsearch and qsort are standard C functions.  bsearch searches an array to find an entry, and qsort sorts an array.

0
RONSLOWCommented:
I am just seeing if I can add a comment -- EE has been having some troubles !!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.