Download File

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
Transcript
Tutorial 2 Control Flow: Loops
NUS SCHOOL OF COMPUTING
CS1010E PROGRAMMING METHODOLOGY
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
1
Quick Summary
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
2
Q1: Program Tracing
Trace the program shown on the left for
i = 0 to i = 30;
What is the output if n = 321;
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
3
Q1: Program Tracing
i
count
2
count
3
count
5
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
n
0
?
0
0
0
4
Q1: Program Tracing
i
count
2
count
3
count
5
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
n
0
?
0
0
0
5
Q1: Program Tracing
i
count
2
count
3
count
5
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
n
0
?
0
0
1
6
Q1: Program Tracing
i
count
2
count
3
count
5
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
n
0
?
0
0
1
7
Q1: Program Tracing
i
count
2
count
3
count
5
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
n
0
?
0
1
1
8
Q1: Program Tracing
i
count
2
count
3
count
5
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
n
1
?
0
1
1
9
Q1: Program Tracing
We then observe that:
count5: numbers divisible by 5
count3: numbers divisible by 15
count2: numbers divisible by 2
BUT not divisible by 10!
We observe that:
count5 counts for:0,5,…,320 count3
counts for:0,15,…,315
count2 counts for:2,4,6,8,12,…,
CS1010E TUTORIAL SLIDES
Hence, when n = 321:
count5 = 320 − 0 ÷ 5 + 1 = 65
count3 = 315 − 0 ÷ 15 + 1 = 22
count2 = 320 − 2 ÷ 2 + 1 − 320 − 0 ÷
10 = 128
PREPARED BY WU CHAO
10
Q2: Program Tracing
Given the program on the right, what
do you think will happen?
What will actually happen?
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
11
Q2: Program Tracing
We observe that:
The value of i will change in the
following pattern:
1, 2, 3, …, …, …,
All values satisfy i>0, the program will
end in infinite loop.
However…
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
12
Q2: Infinite Loop & Data Type Range
1. The program ends up in value -2147483648 after a short while.
2. Integer data type has a finite range on your computer, from
2147483648 to -2147483648.
3. Incrementing from 2147483648 causes overflow and it will
immediately jump to the negative extreme.
-2147483648
CS1010E TUTORIAL SLIDES
0
PREPARED BY WU CHAO
2147483648
13
Q3: Sum of Sequence and Series
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
14
Q3: Sum of Sequence and Series
Sum = 1 + 2 + 3 + … + n
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
Sum = n + (n-1) + … + 3 + 2 + 1
15
Q3: Sum of Sequence and Series
while(n--){…}
This structure is frequently used as a simple loop
to repeat n times.
Important: you can only use this method if you
value of n is not used again later on.
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
16
Q3: Sum of Sequence and Series
Note:
Value of “i”: 1, 3, 5, …,
Value of “j”: 1, -1, 1, -1, …,
Value of “i*j”: 1, -3, 5, -7, …,
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
17
Q3: Sum of Sequence and Series
4.0*j/i:
Differentiate between integer division and
floating point division:
4/5 = 0;
4.0/5 = 0.800000;
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
18
Q3: Sum of Sequence and Series
The difference between 𝜋𝑛 and 𝜋𝑛−1
depends only on the last term added
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
19
Q4: Printing 2D Matrix
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
20
Q4: Printing 2D Matrix
i
j
j
j
j
j
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
21
Q4: Printing 2D Matrix
%4d:
The output number will take up
altogether 4 spaces.
If the number need more than 4
spaces, just print normally.
If the number need less than 4,
add blank spaces in front.
Challenge: How to print inverted triangle?
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
22
Q5: Prime Number Test
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
23
Q5: Prime Number Test
We only need to test until the integer value
of 𝑛 to determine if n is prime
Exhaustively test from 2 to k
Check end configuration, if “i
<= k”, it means it did not pass
through all tests.
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
24