Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1998-04-15
6
Medium Priority
?
278 Views
Last Modified: 2010-04-15
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
Comment
Question by:zaya69
  • 5
6 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 380 total points
ID: 1258131
use bsearch
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1258132
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1258133
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:zaya69
ID: 1258134
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1258135
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1258136
I am just seeing if I can add a comment -- EE has been having some troubles !!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

972 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