Counting items in a struct array
Author Message
Counting items in a struct array

Hello all,

I'm working on a homework assignment in which we read in a binary file to an
array of structures, then use the data to do some calculations.  I've gotten
all of the problems figured out except one.

We need to find the name of the city and the state that have the most people
living there.  The struct we are using has this format:

struct Person
{
char name[80];
int age;
double height;
char city[30];
char state[3];

Quote:
}

I'm at a loss on how to keep a count of the different cities and states, as
it could be almost limitless.

Any thoughts?  Clues?  I can post my code if needed, but I really haven't
gotten that far on this problem.

I'm not looking for code samples necessarily, just a nudge in the right
direction.

Thanks for any help in advance.

Best regards,

-craig

Fri, 14 Oct 2005 09:10:12 GMT
Counting items in a struct array

Quote:

> Hello all,

> I'm working on a homework assignment in which we read in a binary file to an
> array of structures, then use the data to do some calculations.  I've gotten
> all of the problems figured out except one.

> We need to find the name of the city and the state that have the most people
> living there.  The struct we are using has this format:

> struct Person
> {
>  char name[80];
>  int age;
>  double height;
>  char city[30];
>  char state[3];
> }

> I'm at a loss on how to keep a count of the different cities and states, as
> it could be almost limitless.

> Any thoughts?  Clues?  I can post my code if needed, but I really haven't
> gotten that far on this problem.

> I'm not looking for code samples necessarily, just a nudge in the right
> direction.

The simplest way would be to create a list of structs containing a city
identifier and a count.  Each time you read in a city, see if it is
already in the list.  If so, increment the count.  If not, add it.
After that, you can get fancy and use a tree or hash table instead of a
list, or improve the search method.

Fri, 14 Oct 2005 09:24:54 GMT
Counting items in a struct array

Quote:
> > struct Person
> > {
> >  char name[80];
> >  int age;
> >  double height;
> >  char city[30];
> >  char state[3];
>  struct Person info [MAX];
> The simplest way would be to create a list of structs containing a city
> identifier and a count.  Each time you read in a city, see if it is
> already in the list.  If so, increment the count.  If not, add it.
> After that, you can get fancy and use a tree or hash table instead of a
> list, or improve the search method.

Ok.  That helps a little bit, but I'm unsure on the implementation.  We
haven't really gone over trees, hash tables or really any data structures
other than structs.  This now seems to be a real limiting factor in
completing this assignment.

I'm guessing I need to do a for loop and go through all of the data in my
structure array, correct?  I'm also guessing that my code would look
something like this:  (Don't laugh, please)

void FindMaxPop (records)  //records is a count of how many records I have
in the array
{

int i;

struct population
{
char city[30];
char state[3];
int count;

Quote:
};

struct population most[MAX]  //max is defined as a global as 101

for (i = 0; i < records; i++)
{
I need help here
};

return void

Quote:
}

-craig

Fri, 14 Oct 2005 10:44:51 GMT
Counting items in a struct array

Quote:

> > > struct Person
> > > {
> > >  char name[80];
> > >  int age;
> > >  double height;
> > >  char city[30];
> > >  char state[3];
> >  struct Person info [MAX];
> > The simplest way would be to create a list of structs containing a city
> > identifier and a count.  Each time you read in a city, see if it is
> > already in the list.  If so, increment the count.  If not, add it.
> > After that, you can get fancy and use a tree or hash table instead of a
> > list, or improve the search method.

> Ok.  That helps a little bit, but I'm unsure on the implementation.  We
> haven't really gone over trees, hash tables or really any data structures
> other than structs.  This now seems to be a real limiting factor in
> completing this assignment.

> I'm guessing I need to do a for loop and go through all of the data in my
> structure array, correct?  I'm also guessing that my code would look
> something like this:  (Don't laugh, please)

> void FindMaxPop (records)  //records is a count of how many records I have
> in the array
> {

> int i;

> struct population
> {

struct population* next;

Quote:
>   char city[30];
>   char state[3];
>   int count;
> };
> struct population most[MAX]  //max is defined as a global as 101

Using an array will limit the number of cities you can count.  If you
know that there is such a limit, fine, but otherwise use a list:

struct population *citylist = 0;

Also, if you want to use the list of cities outside of this function, it
should be declared outside.

Quote:
> for (i = 0; i < records; i++)
>   {
>         I need help here

struct population *p = citylist;
while ( p ) {
if p->city and p->state match current record {
increment p->count
break
}
increment p
Quote:
}

if p == 0 {
allocate space for new struct population
fill it with city and state from current record
set next to 0
set count to 1
new->next = citylist
citylist = new

Quote:
}
>   };

Now, assuming the list lives only in this function, you must free it.

Quote:
>  return void

Just return, or omit entirely.

- Show quoted text -

Quote:

> }

Fri, 14 Oct 2005 11:15:24 GMT
Counting items in a struct array

Quote:

> Hello all,

> I'm working on a homework assignment in which we read in a binary file to an
> array of structures, then use the data to do some calculations.  I've gotten
> all of the problems figured out except one.

> We need to find the name of the city and the state that have the most people
> living there.  The struct we are using has this format:

> struct Person
> {
>  char name[80];
>  int age;
>  double height;
>  char city[30];
>  char state[3];
> }

> I'm at a loss on how to keep a count of the different cities and states, as
> it could be almost limitless.

> Any thoughts?  Clues?  I can post my code if needed, but I really haven't
> gotten that far on this problem.

> I'm not looking for code samples necessarily, just a nudge in the right
> direction.

> Thanks for any help in advance.

You should probably think of setting up structs for cities and states,
each containing a name and a population count.  You loop through your

Is this person's city in the array of city structs already?
If yes, increment the population count for that city.
If no, add that city to the array, with a population count of 1.

Within the person loop, do the same for states.

Keep a count of the number of cities, and the number of states, in
the respective arrays.

Write a look-up function for the city struct array, which returns
the array index of the city if the city name matches one already
entered, or (say) -1 if there's no match.  And the same for the
states.

How to handle the arrays of structs?  If you have studied malloc
already you can use that.  Alternatively, just set up suitably
sized static arrays (maybe 1000 cities, 60 states) and check for
overflow.

Once you've finished looping through the person records, scan
the city and state arrays to identify which have the highest
population counts.

--
Allin Cottrell
Department of Economics
Wake Forest University, NC

Fri, 14 Oct 2005 11:26:16 GMT
Counting items in a struct array

Quote:

> I'm working on a homework assignment in which we read in a
binary file to an
> array of structures, then use the data to do some
> calculations.
I've gotten
> all of the problems figured out except one.

> We need to find the name of the city and the state that have
the most people
> living there. The struct we are using has this format:

> struct Person
> {
>  char name[80];
>  int age;
>  double height;
>  char city[30];
>  char state[3];
> }

> I'm at a loss on how to keep a count of the different cities and states, as
> it could be almost limitless.

> Any thoughts?  Clues?  I can post my code if needed, but I really haven't
> gotten that far on this problem.

> I'm not looking for code samples necessarily, just a nudge in the right
> direction.

How about using an array of state structures, each of which has
an associated linked list of city structures (Do you suppose
there might be some advantage to inserting nodes into the city
lists in alphabetical order)?

--
Morris Dovey
West Des Moines, Iowa USA

Fri, 14 Oct 2005 11:33:22 GMT
Counting items in a struct array

Quote:
> > > struct Person
> > > {
> > >  char name[80];
> > >  int age;
> > >  double height;
> > >  char city[30];
> > >  char state[3];
> >  struct Person info [MAX];
> > The simplest way would be to create a list of structs containing a city
> > identifier and a count.  Each time you read in a city, see if it is
> > already in the list.  If so, increment the count.  If not, add it.
> > After that, you can get fancy and use a tree or hash table instead of a
> > list, or improve the search method.

> Ok.  That helps a little bit, but I'm unsure on the implementation.  We
> haven't really gone over trees, hash tables or really any data structures
> other than structs.  This now seems to be a real limiting factor in
> completing this assignment.

I would probably use a hash table, but another way that I have done problems
like this is to sort the list.  Once sorted, it is pretty easy to go down
the list and count them.

qsort() will sort the list for you pretty easily, though I don't know if you
have covered that in class.

-- glen

Fri, 14 Oct 2005 15:20:23 GMT
Counting items in a struct array
Hey Gang,

I got this one solved by using an additional array of structs.

You folks are great!!

-craig

Quote:
> Hello all,

> I'm working on a homework assignment in which we read in a binary file to
an
> array of structures, then use the data to do some calculations.  I've
gotten
> all of the problems figured out except one.

> We need to find the name of the city and the state that have the most
people
> living there.  The struct we are using has this format:

> struct Person
> {
>  char name[80];
>  int age;
>  double height;
>  char city[30];
>  char state[3];
> }

> I'm at a loss on how to keep a count of the different cities and states,
as
> it could be almost limitless.

> Any thoughts?  Clues?  I can post my code if needed, but I really haven't
> gotten that far on this problem.

> I'm not looking for code samples necessarily, just a nudge in the right
> direction.

> Thanks for any help in advance.

> Best regards,

> -craig

Fri, 21 Oct 2005 09:11:04 GMT

 Page 1 of 1 [ 8 post ]

Relevant Pages