Date post: | 23-Dec-2015 |
Category: |
Documents |
Upload: | noel-stevens |
View: | 236 times |
Download: | 0 times |
1
Tutorial 9 String and StructureNUS SCHOOL OF COMPUTING
CS1010E PROGRAMMING METHODOLOGY
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
3
Q1: Self Implementation of <string.h>
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
5
Q1: Self Implementation of <string.h>
R A D A R \0
s1
s2
t
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
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
8
Q2: Christmas Carol
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 ”
10
Q2: Christmas Carol
CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
11
Q2: Christmas Carol
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
13
Q2: Christmas Carol
CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
14
Q3: Overlapping Rectangles
CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
15
Q3: Overlapping Rectangles
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
17
Q3: Overlapping Rectangles
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:
19
Q3: Overlapping Rectangles
CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
20
Q4: Snapback Problem
CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
21
Q4: Snapback Problem
CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
22
Q4: Snapback Problem
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
24
Q4: Snapback Problem
CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
25
Q4: Snapback Problem
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.