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

Sat, 15 Oct 2005 23:45:11 GMT
Counting items in a struct array

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

write a compare function to order your array of data by city or state (I
assume that you are reading in all the data to some kind of container).
Now use qsort. Once you have all people living in the same city
consecutively it should be easy to solve the problem.

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--

Sun, 16 Oct 2005 07:20:23 GMT
Counting items in a struct array

Quote:

> We need to find the name of the city and the state that have the most people
> living there.
[...]
> I'm at a loss on how to keep a count of the different cities and states, as
> it could be almost limitless.

Since this is homework, I'll only give you hint: you don't need to
know that number.
--

Even if all the snow were burnt, ashes would remain.
--

Sun, 16 Oct 2005 07:20:33 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.

You imply you have created an array somewhere, something like:

struct Person everybody[MAXPEOPLE];

somewhere, and know how many slots are filled, with something
like:

size_t firstemptyslot;

The simplest (but far from the most efficient) way is to add a
flag field to the struct Person, something like "int flag;", and a
routine to clear it:

for (i = 0; i < firstemptyslot; i++) everybody[i].flag = 0;

Then, start with everybody[0], set the count for that city to 1,
set the flag, and scan the rest for the same city.  If found, set
the flag and increment the count.

Advance to the next unflagged entry in everybody, and repeat the
process.  Do it all over again for states.

A better way would be to use database and hashtable techniques for
the input data, but that is more complex.  It would minimize total
storage, and develop the results on the fly during readin.

--

Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>  USE worldnet address!
--

Sun, 16 Oct 2005 07:20:39 GMT
Counting items in a struct array

Quote:

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

On Unix, one could print the state concatenated with the city to
a text file (one city:state per line), sort it (to bring all
records having the same city:state together), pipe that through
uniq -c (to prefix a count to the unique records), pipe that
through sort -rn to sort in descending numerical order, then
pipe through sed 1q to print the first line (greatest frequency).

If you need to write all your own code, perhaps this at least
inspires a solution.
--

Sun, 16 Oct 2005 07:20:50 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.

> Best regards,

> -craig

Craig,

Since you stated in the beginning that you are reading in these records
to start with, simply keep a counters in another array of structures.
Create a struct that has the city name, the state, and the current total
population for that city/state.
As you read in a person:
if the city/state doesn't exist,
allocate a new struct for them
increment the counter for the city/state for that person.
(As this is an assignment, I'll leave the details up to you ; )

-Glenn
--

Sun, 16 Oct 2005 07:21:09 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages