Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Tutorial 2 Structures II AACS1084 PCDII

1. Write C statements for the following instructions:


(i) Declare a tagged structure called Prod that contains integer variables id and year, string variable name
and double variable price.
(ii) By referring to the tagged structure Prod declared in (i) above, declare a 3-element array of this
structure and call it product. At the same time, initialize it with these values.
id = 1111, 2222, 3333
year = 2011, 2012, 2010
name = AAA, BBB, CCC
price = 12.50, 50.00, 38.50 id year name price
product[0]
(iii) The diagram gives you an idea of the array of
structures product. What are the values of the product[1]
following after the initialization?
product[1].year product[2]
product[2].id
product[0].name

2. Assume the following declaration has been made.


typedef struct {
int day;
int month;
int year;
} Date;

Use typedef to declare a structure called Personal which has the following members:
 An array called name of type character with 20 elements.
 A variable called sex of type character.
 A variable called birthday of the structure type declared above (nested).
Declare a variable called strong of the structure type that you have just created.

3. The XYZ Company keeps the following records of its employees:


Field Array size (if applicable) Example of data
employeeName Maximum 30 characters Abdul Samad
employeeId Maximum 10 characters PBCS00001
address Maximum 35 characters 15, Lorong 1, Taman Aman, Penang
phoneNumber Maximum 15 characters 012-4289156
salary 1234.56

(i) Declare a tagged structure called Employee to hold the above information.

(ii) Define an array emp of type Employee (as in (i) above) that can store 100 sets of employee information.

(iii) Write statements to accept 100 sets of employee data and store them all into the array declared in (ii).

(iv) Write a statement to change the employeeName of the last employee in the array emp in (ii) to
“Anthony Swamy”.

(v) Use typedef to declare a structure called Boss with the following members (including a nested structure):
 An array called departmentCode of type character with 6 elements.
 An array called managerName of type character with 20 elements.
 A variable employee of the type struct Employee (defined in (i) above).
Define a variable called boss of the structure type you have just declared.
---------------------------------------------------------------------------------------------------------------------------------------
Page 1 of 3
Tutorial 2 Structures II AACS1084 PCDII
4.
(i) Using typedef, declare a structure called Employee which can hold the following details of an employee.
ID
Name
Position (3m)

(ii) Using the keyword struct (without typedef), declare a structure called Wrk to store the employee
working record consisting of the following details.
(You must make use of the structure declared in part (i) above.)
emp (as in (i) above)
rate
hours
basic (4m)

(iii) Define an array, called empWrk, of 10 structures of type Wrk declared in part(ii) above. Initialize the
first two records with details as follows:

ID Name Position Rate Hours Basic


771122 Mike Wong Programmer 4.62 40 1800.00
345906 Bill John Manager 8.51 38 2400.00
(4m)
(iv) Write a program segment to display the name and pay for employee Bill John. The formula for pay is:
(4m)
Pay = Rate * Hours + Basic

5.
(i) Using typedef, declare a structure called Schedule which can hold the following details of a class
schedule.
subject (e.g. PROGRAMMING)
time (e.g. 10)
day (e.g Monday) (4m)

(ii) Using the keyword struct (without typedef), declare a structure called Address to store an address
consisting the following strings:
street
city
state
zip (4m)

(iii)Use typedef to declare a structure called StudentData that can be used to store the following student data.
You must make use of the structures declared in part (i) and (ii) above.
Registration Number (string)
Address (as in (ii) above)
GPA (double)
Schedule ( for six different classes) (as in (i) above) (4m)

(iv) Define a variable stud1 of the type declared in part (iii) above. (1m)

(v) Write a statement to assign the string ”PROGRAMMING” to the member subject in the first class
schedule in stud1. (2m)

(vi) Write a statement to assign the value 10 to the member time in the last class schedule in stud1. (1m)

---------------------------------------------------------------------------------------------------------------------------------------
Page 2 of 3
Tutorial 2 Structures II AACS1084 PCDII
6.
(i) Declare a tagged structure called Date which consists of the following members: (2m)
month (eg. 12)
day (eg. 5)
year (eg. 2001)
A graphic representation of the structure is shown below:
Date
month day year

(ii) Using typedef, declare a structure called Student with the following members: (4m)
studentName (eg. Wilson Tan Wing Soon)
icNo (eg. 980225-06-0025)
birthDate (eg. as in (i) shown above)
A graphic representation of the structure is shown below:
Student
studentName icNo birthDate
month day year

(iii) Using typedef, declare a structure called Programme which is used to store data about the
programme. The structure must contain the following details: (5m)
programmeTitle (eg. Diploma in Computer Studies)
commencingDate (eg. as in (i) above)
studentData (eg. as in (ii) above, there are 10 of these)
A graphic representation of the structure is shown below:

Programme
programmeTitle commencingDate
month day year

studentData
studentName studentName studentName

icNo icNo icNo


...
birthDate birthDate birthDate
...
month day year month day year month day year

[0] [1] ... [9]

(iv) Define a variable called ibProgramme of the type Programme which was declared in (iii). (1m)

(v) Write a C program segment that prompts the user to input 10 sets of student records. Store the data
entered into the member studentData in the variable ibProgramme defined in (iv). (9m)

---------------------------------------------------------------------------------------------------------------------------------------
Page 3 of 3

You might also like