+ All Categories
Home > Documents > Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E...

Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E...

Date post: 23-Dec-2015
Category:
Upload: noel-stevens
View: 236 times
Download: 0 times
Share this document with a friend
Popular Tags:
26
Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
Transcript
Page 1: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

1

Tutorial 9 String and StructureNUS SCHOOL OF COMPUTING

CS1010E PROGRAMMING METHODOLOGY

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 2: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

2

Quick Summary1. Structure introduces the concept of an Object. The Object can

contain many attributes. (Person: Gender, Age, etc.)1. Two ways to declare a structure:

typedef struct{…}[identifier];struct [identifier]{…};

2. To retrieve the attributes of a structure use “.” operator:Person.Gender = “male”;Person.Age = 20;

3. For Structures with pointers:Person->Gender = “male”;Person->Age = 20;

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 3: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

3

Q1: Self Implementation of <string.h>

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 4: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

4

Q1: Self Implementation of <string.h>

R A D A R \0

Left Rightstrlen = right – left;

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 5: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

5

Q1: Self Implementation of <string.h>

R A D A R \0

s1

s2

t

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 6: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

6

Q1: Self Implementation of <string.h>

R A D A R \0

R A \0

s1

s2

t

my_strcpy(s1,s2);

R A R A D A R \0

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 7: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

7

Q1: Self Implementation of <string.h>

R A D A R \0

R A D A R \0

s1

s2

Loop until (s1 becomes \0) or (a difference is found):

Return the difference

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 8: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

8

Q2: Christmas Carol

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 9: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

9

Q2: Christmas Carol

On any particular day:

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Constant strings:

“On the ”

“day of Christmas,\n”

“my true…\n”

“and ”

Page 10: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

10

Q2: Christmas Carol

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 11: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

11

Q2: Christmas Carol

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 12: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

12

Q2: Christmas Carol

Because a word ends with either of the following:Space, Comma, Fullstop.

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 13: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

13

Q2: Christmas Carol

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 14: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

14

Q3: Overlapping Rectangles

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 15: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

15

Q3: Overlapping Rectangles

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 16: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

16

Q3: Overlapping Rectangles

If the points do not fulfil the pre-condition required, we just need to swap the two points.

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 17: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

17

Q3: Overlapping Rectangles

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 18: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

18

Q3: Overlapping Rectangles

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

It is easier to check if the two rectangles do not overlap:

Page 19: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

19

Q3: Overlapping Rectangles

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 20: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

20

Q4: Snapback Problem

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 21: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

21

Q4: Snapback Problem

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 22: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

22

Q4: Snapback Problem

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 23: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

23

Q4: Snapback Problem

Bubble sort algorithm is used.This is an improved version of the bubble sort.What is the difference?

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 24: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

24

Q4: Snapback Problem

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 25: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

25

Q4: Snapback Problem

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Page 26: Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

26

Q4: Snapback Problem

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

1 0 0 1 1 … 1 1 0

In the binary sequence, 1 means take the item number j, 0 means omit the item number j.

We use this binary sequence approach to enumerate all the possible ways to take the items available and compare the value.


Recommended