Title: SOFTWARE DEVELOPMENT IN C CM9041
1SOFTWARE DEVELOPMENT IN CCM904-1
- Lecture 5
- Searching and sorting
- Data Structures
2This week
- Structs
- Typedef
- Searching
- Sorting
- Examples using structs
3Parallel Arrays
Corresponding position in each array refers to a
different piece of data which is an item of data
belonging to the same logical entity e.g.
Name (string) Age (integer) Grade
(character) Number of Attendances (int)
0 1 2 3 4 5
4Data Collections Involving Multiple Types
- Requires of an array of structures capable of
holding complex data. Referred to in C as a
struct - Each element of the array contains a collection
of data of the same format.
Name Age Grade Attendances
5Array of Structs
An array of structs
Name Age Grade Attendances
Name Age Grade Attendances
Name Age Grade Attendances
Name Age Grade Attendances
0 1 2 3
Studentarr2
Studentarr0
The definition of each element is referred to as
a Struct
6Arrays of Structs
- A Struct is a collection of data variables under
one name - Each constituent variable may be of a different
data type - Each part of the Struct can be accessed directly
- A Struct is user-defined
- Variables are declared as being instances of the
user-defined type in the same way that variables
of pre-defined types are declared (using a
slightly different notation however!!) - An array can be composed of Structs to give an
indexed collection of objects that consist of
mixed data - studentarray1 student (copy a struct into a
position in an array) - studentarray1.age 21 (update part of an
element of an array of structs
7Defining a struct
Here is an example structure definition.
typedef struct char name64, course128
int age, year student This
defines a new type student. Variables of type
student can then be declared as
follows. student st_record
8Arrays of Structures
- An array called birthdays of the same data type
as the structure date can be created thus -
- struct date birthdays5
-
- This creates an array of 5 elements each of type
date. - Assignments and operations can be carried out
thus e.g - birthdays1.month 12
- --birthdays1.year
9Variations in declaring structures
- struct date
-
- int month, day, year
- todaysdate, purchase_date
- struct date
-
- int month, day, year
- todaysdate 25,9,1985
-
- struct date
-
- int month, day, year
- dates100
10Structures containing structures
- Structures can also contain structures.
-
- struct date
- int month, day, year
-
-
- struct time
- int hours, mins, secs
-
-
- struct date_time
- struct date sdate
- struct time stime
-
11typedef
- A way of identifying a programmer-defined data
type - Avoids proceeding all struct declarations with
the word struct - Allows struct to be passed as a parameter into a
function - Variables can be created of a type once it has
been defined using typedef
12Searching and sorting
- Lists of data often stored in arrays
- May be complex data
- Use arrays of structs
- Many standard processes require searching and
sorting
13Linear Search
- Suitable for sorted or unsorted list
- Starts at beginning of array and checks each
element against a target - Loops for number of items in the list
- Returns position in array of found item or value
representing not found - Position used to retrieve data
14Linear Search
Index 0
Index 1
Index 2
Index 3
15Linear Search
Index 0
Index 1
Index 2
Index 3
Index 4
1
34
28
10
9
81
1
34
28
10
9
81
Index 5
16Searching an array of structs
- Same process
- Use one of the fields to search
- e.g. birthdayslistindex.month
- Returns position in array as before
17Bubble Sort
- Sort needed for variety of reasons
- Can be ascending or descending
- Sorted data often required for more efficient
searching algorithms - Bubble sort one of the simplest
- Each complete pass results in the highest
unsorted item bubbling to the end.
18First Pass
Compare 0,1
Compare 1,2
Compare 2,3
Compare 3,4
Compare 4,5
19Second Pass
Compare 0,1
Compare 1,2
Compare 2,3
Compare 3,4
20Third Pass
Compare 0,1
Compare 1,2
Compare 2,3
21Sorting array of structs
- Same principle as for single data arrays
- One of the fields is used to perform the
comparison - E.g.
- birthdays i.month gt
- birthdaysi1.month
- If a swap is needed the whole structs are swapped
22Next week
- More on functions
- Introduction to pointers
- Passing data between functions
- Assignment help