0% found this document useful (0 votes)
0 views

lap-trinh-c_cao-tuan-dung_week14-structure

Uploaded by

Ngoc Lequoc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

lap-trinh-c_cao-tuan-dung_week14-structure

Uploaded by

Ngoc Lequoc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C Programming

Introduction

week 14:Structure

om
Lecturers : Cao Tuan Dung

.c
Dept of Software Engineering
Hanoi University of

ng
Technology co
For HEDSPI Project
an
th
ng

Topic of this week


o
du

• Structure
u

– Class Lecture Review


cu

• Structure declaration
• Using typedef
• Accessing field of structure variables.
– Programming Exercises

1
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Structure
• A structure in C is a collection
of items of different types.

• Structures, or structs, are very


useful in creating data

om
structures larger and more
complex than the ones we

.c
have discussed so far.

ng
co
an
th
ng

Defining a struct in C
o
du

struct struct-name
u

{
cu

field-type1 field-name1;
field-type2 field-name2;
field-type3 field-name3;
...
};

2
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Example
• We can define a type for
representing a student who has a
name, age and a grade like this
struct student {

om
char name[20];
int age;

.c
float grade;
};

ng
co
an
th
ng

Example
o
du

• To create a new user defined type for


cars:
u

– A car must have a brandname, a model


cu

(string) and a year of production (a number)

struct car {
char* make;
char* model;
int year;
};

3
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Variable declaration and
Initialisation
• You must use keyword struct in the
declaration
struct student s1;
struct car mycar;

om
struct student s1 = (“Nguyen Le”, 19, 8.0);

.c
struct car mycar = (“Fiat”, "Punto", 2004);

ng
co
an
th
ng

Structure declaration with


typedef
o
du

typedef struct student {


char name[20];
u
cu

int age;
float grade;
} student_t; Now the program
has a new types -
student_t and
typedef struct car { car_t
char* make;
char* model;
int year;
}car_t;

4
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Variable declaration
• With the usage of typedef, we don’t
have to write “struct student”
every time!
• For example, we can define two

om
complex numbers in the following
line:

.c
car_t mycar;
student_t excellentP;

ng
co
an
th
ng

Accessing Members of a
Structure
o
du

• Use a dot between the structure


u

name and the field name .


cu

car_t mycar;
mycar.year = 2004;

student_t excellentp;
excellentp.age = 18;
excellentp.grade = 7.8;

5
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercises 14.1
a) Create a structure named Date for storing date concerning
variables. Each date has a day, a month and a year.
b) Write a function for the input of variable of this type.
Remember to check the validation of data
c) Write a function to datecmp to compare two date which
return
-1 if the first date (parameter) is before the second

om
0 if two date are identical.
1 if the first date (parameter) is after the second
d) Write a program asking user to for two date and print out

.c
the results of the comparison.

ng
For example: 2/10/1997 is after 23/8/1997
co
an
th
ng

Exercise 14.2
o
du

• Write a program that uses a structure to store


the following weather data for a particular
month:
u

– Total Rainfall
cu

– High Temperature
– Low Temperature
– Average Temperature
• The program should have an array of 12
structures to hold weather data for an entire
year. When the program run, it ask the user to
enter data for each month and then calculate and
display the average rain fall, the total rainfall of
the year, the highest and lowest temperatures for
the year.
• Input validation: Only accept temperature within
the range -40 and 50 degrees Celcius.

6
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 14.3
• Write a student management program using this
structure:

typedef struct
{
char id[6];
char name[31];
float grade;
char classement

om
} student;

Students are classified according to their grade in

.c
respect to this criteria :
– from 9 to 10: A (Excellent)
– from 8 to 9: B (Good)

ng
– from 6.5 to 8: C (Medium)
– < 6.5 : D (Bad) co
an
th
ng

Exercise 14.3
o
du

• The program should read from


keyboard data for n students,
u

then print the list of student in


cu

order descending of grade like


this:
Name Grade Classment
Dao Tiem 9.3 A
Dinh Lan 8.2 B
Bui Luu Van 5.7 D

7
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 14.4
• Define a new type for representing
the fractions. Then use it to write a
fraction manipulation program. This
program have the following
functionalities.

om
– Input for an array of fraction
– Print the content of the fraction array

.c
– Inverse all the fraction in the array
– Compare two fraction

ng
co
an
th
ng

Exercise 14.5
o
du

• Develop others following userful


fraction manipulation functions:
u

– convert a fraction to fraction simple


cu

– mutiply, add two fractions


• And integrate them into the program
in exercise 14.4

8
CuuDuongThanCong.com https://fb.com/tailieudientucntt

You might also like