SOFTWARE DEVELOPMENT IN C CM9041 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

SOFTWARE DEVELOPMENT IN C CM9041

Description:

An array called birthdays of the same data type as the structure date can be created thus: struct date birthdays[5]; This creates an array of 5 elements each of ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 23
Provided by: rho2
Category:

less

Transcript and Presenter's Notes

Title: SOFTWARE DEVELOPMENT IN C CM9041


1
SOFTWARE DEVELOPMENT IN CCM904-1
  • Lecture 5
  • Searching and sorting
  • Data Structures

2
This week
  • Structs
  • Typedef
  • Searching
  • Sorting
  • Examples using structs

3
Parallel 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
4
Data 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
5
Array 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
6
Arrays 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

7
Defining 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
8
Arrays 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

9
Variations 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

10
Structures 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

11
typedef
  • 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

12
Searching and sorting
  • Lists of data often stored in arrays
  • May be complex data
  • Use arrays of structs
  • Many standard processes require searching and
    sorting

13
Linear 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

14
Linear Search
  • Target value 10

Index 0
Index 1
Index 2
Index 3
  • Position found 3

15
Linear Search
  • Target value 101

Index 0
Index 1
Index 2
Index 3
Index 4
1
34
28
10
9
81
1
34
28
10
9
81
Index 5
  • Position found -1

16
Searching an array of structs
  • Same process
  • Use one of the fields to search
  • e.g. birthdayslistindex.month
  • Returns position in array as before

17
Bubble 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.

18
First Pass
Compare 0,1
Compare 1,2
Compare 2,3
Compare 3,4
Compare 4,5
19
Second Pass
Compare 0,1
Compare 1,2
Compare 2,3
Compare 3,4
20
Third Pass
Compare 0,1
Compare 1,2
Compare 2,3
21
Sorting 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

22
Next week
  • More on functions
  • Introduction to pointers
  • Passing data between functions
  • Assignment help
Write a Comment
User Comments (0)
About PowerShow.com