Solved

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

Posted on 1998-04-15
6
234 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 190 total points
Comment Utility
use bsearch
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:zaya69
Comment Utility
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
Comment Utility
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
Comment Utility
I am just seeing if I can add a comment -- EE has been having some troubles !!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

771 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

10 Experts available now in Live!

Get 1:1 Help Now