Course: Programming Fundamentals (C language)
LAB 3 – struct data types
Main content:
1.
2.
3.
4.
Get used to struct data type definitions
Get used to member accesses with struct data types
Start simple data processing
Check coding styles
Practice:
1. Get used to struct data type definitions
1.1. Define a struct data type to represent the courses that you have studied so far. Each course
has:
- a course identifier which is a string of 6 alphanumeric letters
- a course name which is a string of at most 100 alphanumeric letters
- credit
- a course type which is REQUISITE or SELECTIVE
1.2. Define a struct data type to represent the electronic products in a store. Each product has:
- a product identifier which is a sequence of 8 digits uniquely identifying this product
from others
- a name which is a string with no space and special characters such as ‘.’, ‘;’, ‘!’, ‘@’, ‘#’,
‘$’, ‘%’, ‘^’, ‘&’, ‘*’, ‘(‘, ‘)’, ‘,‘, ‘-’, ‘*‘, ‘+’
- a design model which is a string starting with one capital letter and the rest with digits
- a delivery year which is a valid year in [1990, 2017]
- a unit price in dollar which is a positive floating-point number
- a care type which is FRAGILE or NORMAL
1.3. Define a struct data type to represent the distributors of those products all over the world.
Each distributor has:
- a name which is a string of only alphabetic characters
- a region which is AMERICA, ASIA, or EUROPE
- an address which includes a city name and a country name
- a product identifier which is a sequence of 8 digits for the main product distributed by
this distributor
- a year when the distributor registered for the main product
1.4. Define a struct data type to represent the employees of an organization. Each employee
has:
- an employee identifier which is a sequence of 6 digits
- an employee full name containing only alphabetic characters and white spaces
1
-
a department which includes a department name, a department location which is LEFT,
RIGHT, or CENTER, and a department annual budget
an employee education level which is HIGH, AVERAGE, or LOW
salary
a starting hiring date
1.5. Define a struct data type to represent the tourists registered with a tour agent. Each tourist
has:
- a tourist full name containing only alphabetic characters and white spaces
- a passport number containing only one capital alphabetic character and 7 digits
- nationality
- fee
- a starting date
- an ending date
- the first visiting location which includes a location name, an x coordinate, an y
coordinate
- the last visiting location which includes a location name, an x coordinate, an y
coordinate
- a tour type which is SPECIAL or NORMAL or SALE
2. Get used to member accesses with struct data types
2.1. Define the variables, the printf, and scanf statements appropriate for populating the data
about the courses as follows:
Course identifier Course name
Credit
Course type
CO1003
Intro. To Comp. Prog.
3
REQUISITE
CO1001
Maths
4
REQUISITE
CO1010
Startup
3
SELECTIVE
CO1005
Physics
4
REQUISITE
2.2. Define the variables, the printf, and scanf statements appropriate for populating the data
about the products as follows:
Product
Design
Care
Product name
Delivery year Unit price
identifier
model
type
12345678
HP_Laser_Jet_Printer P1102
2010
12.5
FRAGILE
23456789
Dell_Inspiron
D2016
2012
24.8
FRAGILE
34567890
Laser_Pointer
P2013
2010
2.6
NORMAL
45678901
Samsung_Tab_SPen
T2016
2016
10.9
FRAGILE
2
2.3. Define the variables, the printf, and scanf statements appropriate for populating the data
about the distributors as follows:
Address
Distributor
Product
Region
Year
name
City name
Country name identifier
FPT
ASIA
Ha Noi
Vietnam
23456789
2009
Didong
ASIA
Ho Chi Minh Vietnam
45678901
2010
Amazon
AMERICA
New York
USA
12345678
2005
Global
EUROPE
London
UK
12345678
2008
2.4. Define the variables, the printf, and scanf statements appropriate for populating the data
about the employees as follows:
Department
Employee Full
Education
Salary Hiring date
identifier name Name Location
level
Annual budget
John
123456
HR
CENTER
100.8
HIGH
22.5
20/06/1995
Smith
Peter
234567
S&M
LEFT
150.23
AVERAGE 20.4
01/03/2000
Brown
Alice
345678
S&M
LEFT
150.23
AVERAGE 20.2
03/04/2005
Depp
March
345689
L&P
RIGHT
200.39
LOW
15.6
06/05/2007
Carter
Full
name
John
Smith
Peter
Brown
Alice
Depp
March
Carter
2.5. Define the variables, the printf, and scanf statements appropriate for populating the data
about the tourists as follows:
First location
Last location
Ending
Passport
Nationality Fee
Starting date
date
Name X
Y
Name X
Y
Tour type
B1203457
English
15.3
13/2/2017
18/2/2017
L1
2.4
5
L2
5.2
8.3 SPECIAL
A3910385
English
13.2
23/12/2016
03/1/2017
L1
2.4
5
L3
4.8
7.5 NORMAL
B2384021
English
13
23/12/2016
03/1/2017
L2
5.2
8.3
L4
6.2
8.9 NORMAL
A3830291
English
10.5
28/12/2016
04/1/2017
L3
4.8
7.5
L5
6.5
9.2 SALE
3. Start simple data processing
3.1. Write a C program to enter your grades corresponding to the courses listed in question 2.1.
In addition, ranking is used to show grades in a 4-scale grading system: ‘A’ for grade in *8.5,
10+, ‘B’ for grade in *6.5, 8.5), ‘C’ for grade in *5.0, 6.5), and ‘D’ for grade in *0, 5). Calculate
your weighted average grade. Display your average grade with two precision places and a
corresponding rank using the appropriate expressions: arithmetic expressions, relational
expressions, conditional expressions, and so on.
3
3.2. Write a C program to calculate how many percents of the FRAGILE products there are, how
many products were delivered in 2010, and how many products with the lowest unit price
there are using the appropriate expressions: arithmetic expressions, relational expressions,
conditional expressions, and so on.
3.3. Write a C program to calculate the percentage of distributors in each region, to find the
oldest distributor and the youngest distributor, to calculate how many distributors there are
in Vietnam using the appropriate expressions: arithmetic expressions, relational
expressions, conditional expressions, and so on.
3.4. Write a C program to list all the employees whose education levels are AVERAGE, to
calculate the average salary at each department, to find the department with the highest
annual budget, and to count how many employees have worked for more than five years
using the appropriate expressions: arithmetic expressions, relational expressions,
conditional expressions, and so on.
3.5. Write a C program to find the tourist who has the longest tour, the farthest tour, the tourist
who has the cheapest tour, the tourist who has the most expensive tour, and the most
crowded tour type using the appropriate expressions: arithmetic expressions, relational
expressions, conditional expressions, and so on along with the information given in
questions 1.5 and 2.5.
4. Check coding styles
For each C program that you have written, check coding styles with the following points:
- How have you used blank lines?
- How have you used tabs for alignment?
- How have you used comments in your source code files?
- Above all, how about your naming conventions? What have you named in your source code
files?
4