Questions on Section 19 - Structures

Question 1

I need a structure to hold data about each of my (multitudinous) girlfriends. The structure should include the young lady's name, her date of birth, her hair colour, her telephone number and, of course, three numbers representing her vital statistics. The structure that you create should contain a reference to a structure for holding dates. Add a member function to calculate the age of the young lady by subtracting her date of birth from a date (also stored using the structure) supplied by the user.

Question 2

Take the insect database program (Worked Example 2). There is enough room for ten insects (numbered 0 to 9), but I have only put in 4 (due to laziness). Add data for six more insects. You could also increase the amount of data held on each insect, for instance, indicating whether the insect had a sting, whether it was carnivorous, what colour it was etc.

Now add further code so that the program displays all the insects that are sociable (i.e. live in colonies). You could adapt the program so that the user can specify the profile of the insects to be displayed e.g. those that can fly and are sociable.

Question 3

Write a structure that holds data about boxes. Each box is rectangular and is determined by its length, its width and its height. The structure should also contain variables specifying the box's volume, its surface area and whether or not it has a lid.

Write a function to accompany your structure which takes a box as a parameter and sets its volume variable based upon its height, length and width. The volume is these three multiplied together. Write another function which sets the surface area in the same way (equal to twice the length times the height, plus twice the width times the height, plus the length times the width. If the box has a lid, then you will need to double that last part as well).

Question 4

The tutorial contains a structure to store data about people. Adapt this structure so that it also holds data about whether each person has a job or not. If the person does have a job, then the structure should specify the name of that job, how many hours a week are involved and the salary.

Explain why the structure you have written is rather inefficient, especially for a database where a large proportion of the people are unemployed.

Question 5

This is a rather more efficient way of doing the same thing. You might find it useful to take a look at Worked Example 2 before doing this.

This time I want you to write a structure that deals just with jobs. The structure should contain the name of the job, the hours involved, the salary and a unique code number for that job. For example, 1 might mean "police officer", 2 might mean "teacher" etc. For simplicity's sake, you can assume that all members of each profession are treated equally (i.e. same pay and hours).

Now adapt your "person" structure so that it contains data about the person him/herself and a reference to the code number of the job (0 could mean "unemployed"). In this way we are divorcing the job description from the person and linking the two simply with a code number. This gets over the main inefficiencies that you listed in the previous question.

Write a short routine that might be included in a database that would display the names of everyone who was paid more than £10,000.


Go back Click here to go back