Download PG Reading C Lab Script pdf

Transcript
156 Computer Programming Using C
Revision 1.3
Exercise 11.3: Using Your New Type Name
Create a function which displays all of the details about a student. Each piece of information should
be shown on a separate line. Alter the program to use the function to display the contents of the
structure in place of the final printf statement.
The function might have a prototype like this:
void display_student(student_type student);
11.4 Using Arrays of Structures
At the beginning of this lab the analogy was drawn between a structure and a card with details written
on it. Usually there are many cards, all of the same type, kept together in a filing cabinet of some kind.
So far we have only used one card. In this section we will use multiple cards by creating an array of
structures.
An array of structures is easy to create by using the type that your have defined. An array of five student
records would be declared like this:
student_type students[5];
You can now access individual structures in the array by specifying their index. For example:
students[2].year_of_birth = 1986;
Exercise 11.4: Using an Array of Structures
Edit the “lab11” program so that the user is first asked how many students they wish to enter details
for. This could be a number anywhere between 1 and 10. If the user enters 0 the program should
end. Ask the user for the details of as many students as they specified. The student details should
be stored in structures in an array.
When the user has entered all of the details the program should display all of the information stored
in the array. This should be achieved by calling your display student function repeatedly, in a
loop.
If you do not know how to attempt this, ask one of the demonstrators for help.
11.5 Graphics Option
A structure can be used to store information about a graphical object such as a line, or a shape. Vector
graphics editing programs manipulate information about graphical objects in this way. In this lab you
will store the information for a set of lines in a structure.
A line can be fully defined by:
• its start coordinate (x, y);
• its end coordinate (x, y);
• its colour (an integer value).