• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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.
 
0
zaya69
Asked:
zaya69
  • 5
1 Solution
 
RONSLOWCommented:
use bsearch
0
 
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
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
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
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.

Join & Write a Comment

Featured Post

Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now