Question
The following typedef for personType defines a structure
for holding a person's name and age
The typedef for peoplePointers defines an array of
pointers to personTypes.
#define MAXIMAL_PEOPLE 10
#define MAXIMAL_STRING_LENGTH 128
/*----Define data types */
typedef char string[MAXIMAL_STRING_LENGTH];
typedef struct personTag {
string name;
int age;
} personType;
typedef personType *personPointer;
typedef personPointer peoplePointers[MAXIMAL_PEOPLE];
Write a program that:
- Reads in the names and ages of some people. The personType for
each person will have to be dynamically allocated.
- Writes out the people's information.
Here's what a sample run might look like:
Please enter person name, "exit" to exit : Bill
Please enter Bill's age : 54
Please enter person name, "exit" to exit : Hillary
Please enter Hillary's age : 53
Please enter person name, "exit" to exit : Monica
Please enter Monica's age : 39
Please enter person name, "exit" to exit : exit
Bill is 54
Hillary is 53
Monica is 39
Bonus Question
Extend the personType to also have a pointer to the
personType of their best friend, i.e., a field:
struct personTag *bestFriend;
After reading in the people, add the step:
- For each person read in, reads in the name of their best friend, searches
through the array to find the right personType, and
sets the person's bestFriend field to point to it.
Then a sample run might look like:
Please enter person name, "exit" to exit : Bill
Please enter Bill's age : 54
Please enter person name, "exit" to exit : Hillary
Please enter Hillary's age : 53
Please enter person name, "exit" to exit : Monica
Please enter Monica's age : 39
Please enter person name, "exit" to exit : exit
Who is Bill's best friend? Hillary
Who is Hillary's best friend? Bill
Who is Monica's best friend? Bill
Bill is 54, and his/her best fried is Hillary
Hillary is 53, and his/her best fried is Bill
Monica is 39, and his/her best fried is Bill
Answer