lecture 12 - nus computing - homeooiwt/cs1010/1819s1/slides/cs1010 … · morning. final 30%...
TRANSCRIPT
![Page 1: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/1.jpg)
![Page 2: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/2.jpg)
Lecture 12
Admin MattersRecap and Look Ahead
13 November 2018
![Page 3: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/3.jpg)
Final Assessment27 November 2018
Morning
![Page 4: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/4.jpg)
Final30%
Midterm15%
PE 215%
PE 110%
Assignments30%
![Page 5: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/5.jpg)
ScopeEverything from Unit 1 to Unit 28
![Page 6: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/6.jpg)
FormatSome MCQs
Some Short Questions
![Page 7: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/7.jpg)
Open BookNothing to Memorize
Focus on Understanding and Applying Principles
![Page 8: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/8.jpg)
Assignment 9
![Page 9: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/9.jpg)
university policy: no deadlines during
reading week
![Page 10: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/10.jpg)
no change in deadline
![Page 11: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/11.jpg)
but no late penalty until after 18 November Sunday 23:59
![Page 12: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/12.jpg)
Deadline to Finalize Marks
5 December 20186:00pm
![Page 13: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/13.jpg)
Looking Forward
Teaser to CS2040C
![Page 14: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/14.jpg)
Data Structures
• A collection of data values and the operations that can be applied
• How to organise and manage data for efficient access and modification?
![Page 15: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/15.jpg)
Example: List• An ordered list of numbers
• Can
• create a list• append to the list• remove a number from the list• find the position of a number• destroy a list• print a list
![Page 16: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/16.jpg)
3 6array
last
size = 4
![Page 17: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/17.jpg)
3 6 4array
last
size = 4
append 4
![Page 18: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/18.jpg)
3 6 4array
last
size = 4
append 2
2
![Page 19: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/19.jpg)
3 6 4array
last
size = 8
append 2
2
![Page 20: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/20.jpg)
3 6 4array
last
size = 8
delete 6
2
![Page 21: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/21.jpg)
3 4 2array
last
size = 8
delete 6
![Page 22: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/22.jpg)
AY18/19 Sem 1
What will you learn?
![Page 23: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/23.jpg)
AY18/19 Sem 1
How to write C
![Page 24: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/24.jpg)
AY18/19 Sem 1
How a C program behaves
![Page 25: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/25.jpg)
AY18/19 Sem 1
Tools and techniques to help write good and correct C programs
![Page 26: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/26.jpg)
AY18/19 Sem 1
How to use C to solve computational problems
![Page 27: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/27.jpg)
AY18/19 Sem 1
Learning to write a program that does what you want it to do is actually not
difficult.
Knowing what you want your program to do is the more challenging part!
![Page 28: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/28.jpg)
AY18/19 Sem 1
Tools and techniques to help write good and correct C programs
![Page 29: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/29.jpg)
Tools / Good Practice• clang• vim• bash• lldb*• make*• git*• clang-tidy*
• assertion
• good programming style
• (some) secure programming
• writing documentation
• testing
![Page 30: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/30.jpg)
AY18/19 Sem 1
How to write C
![Page 31: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/31.jpg)
C Language / Syntax• Types
• Functions
• + - * / %
• if else
• && || !
• for / while / do-while
• arrays
• pointers & *
• calloc / free
• #include / #define
• struct
• printf / scanf
![Page 32: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/32.jpg)
Things We Didn’t Cover• FILE I/O
• argc / argv
• enum
• string functions
• bit operations
• separate compilation
![Page 33: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/33.jpg)
AY18/19 Sem 1
How a C program behaves
![Page 34: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/34.jpg)
Behavourial / Mental Model
• machine code
• data in memory
• types
• stack and heap
• call stack
• memory address
• call by value / reference
![Page 35: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/35.jpg)
AY18/19 Sem 1
How to use C to solve computational problems
![Page 36: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/36.jpg)
Problem Solving• Decomposition
• Recursion & Backtracking
• Flowcharts
• Conditionals
• Loop
• Assertion and Invariants
• Arrays and Lists
• Sorting
• Searching
• Efficiency
![Page 37: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/37.jpg)
Computational Thinking
The mental process associated with computational problem solving
![Page 38: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/38.jpg)
1. Decomposition 2. Pattern Recognition 3. Abstraction 4. Algorithms
![Page 39: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/39.jpg)
Decomposition
![Page 40: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/40.jpg)
output max
input k and l0..lk-1
max
output min
input k and l0..lk-1
min
output sum
input k and l0..lk-1
sum
max(L, k) min(L, k) sum(L, k)
![Page 41: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/41.jpg)
Find the Std Dev• Give an algorithm to find the standard
deviation of a given list L of k integers.
sPk�1i=0 (li � µ)2
k
![Page 42: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/42.jpg)
Break it down to subproblems
![Page 43: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/43.jpg)
sPk�1i=0 (li � µ)2
k
sqrtmeansquare
subtract
![Page 44: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/44.jpg)
taxi
![Page 45: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/45.jpg)
double surcharge(long day, long hour, long minute){ if (is_weekday(day) && is_morning_peak(hour, minute)) { return MORNING_SURCHARGE; } if (is_evening_peak(hour)) { return EVENING_SURCHARGE; } if (is_midnight_peak(hour)) { return MIDNIGHT_SURCHARGE; } return 1.0;}
![Page 46: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/46.jpg)
bool is_weekday(long day){ return (day >= 1 && day <= 5);}
bool is_morning_peak(long hour, long minute){ return (hour >= 6 && hour < 9) || (hour == 9 && minute <= 29);}
bool is_evening_peak(long hour){ return (hour >= 18 && hour <= 23);}
bool is_midnight_peak(long hour){ return (hour >= 0 && hour < 6);}
![Page 47: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/47.jpg)
social
![Page 48: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/48.jpg)
bool is_friend(char **network, int i, int j) { if (i >= j) { return network[i][j] == FRIEND; } return network[j][i] == FRIEND;}
![Page 49: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/49.jpg)
/** * Checks if i and j has a common friend * @param[in] n The number of users * @param[in] degree_1 The 1-hop friendship information * @param[in] degree_h The h-hop friendship information * @param[in] i A user * @param[in] j Another user * @return FRIEND if i and j has a (h+1)-hop connection, * STRANGER otherwise. */char is_connected(long n, char **degree_1, char **degree_h, long i, long j) { for (int m = 0; m < n; m += 1) { if (is_friend(degree_1, i, m) && is_friend(degree_h, m, j)) { return FRIEND; } } return STRANGER;}
![Page 50: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/50.jpg)
/** * Computers the h-hop friendship for the whole network. * @param[in] n The number of users. * @param[in] degree_1 The 1-hop friendship network * @param[in] degree_h The h-hop friendship network * @param[out] degree_h1 The (h+1)-hop friendship network */void compute_degree_h(long n, char **degree_1, char **degree_h, char **degree_h1) { for (int i = 0; i < n; i += 1) { for (int j = 0; j <= i; j += 1) { if (is_friend(degree_h, i, j)) { degree_h1[i][j] = FRIEND; } else { degree_h1[i][j] = is_connected(n, degree_1, degree_h, i, j); } } }}
![Page 51: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/51.jpg)
Decomposition
break complex problems down into “bite-size” subproblems
that you can solve
![Page 52: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/52.jpg)
George Pólya said
“If you can’t solve a problem, then there is an easier problem
you can solve: find it”
![Page 53: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/53.jpg)
Solve easier problem, then generalised
![Page 54: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/54.jpg)
e.g., find two hops
neighbors, then generalise to k hops
![Page 55: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/55.jpg)
# # # ## ## # # # # #### #### # # # # ###### ###### # # # # # # # # ## ## ## ## ## ## # # # # # # # # ########## ########## # # # # # # # # ############ ############ # # # # # # # # # # # # ## # ## # ## ## # ## # ## # # # # # # # # # # # # # # # # ################ ################ # # # # # # # # # # # # ################## ################## # # # # # # # # # # # # # # # # ## ## # ## # ## ## ## ## # ## # ## ## # # # # # # # # # # # # # # # # # # # # ###################### ###################### # # # # # # # # # # # # # # # # #### #### #### #### #### #### #### #### #### #### # # # # # # # # # # # # # # # # # # # # # # # # ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## # # # # # # # # # # # # # # # # # # # # # # # ############################# ############################
e.g., draw left most cells, then draw the rest.
![Page 56: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/56.jpg)
Pattern Recognition
![Page 57: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/57.jpg)
pattern
![Page 58: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/58.jpg)
![Page 59: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/59.jpg)
vote
![Page 60: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/60.jpg)
double percentage_of_nixon(long nixon, long total) { return nixon*100.0/total;}
double percentage_of_mcneal(long mcneal, long total) { return mcneal*100.0/total;}
![Page 61: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/61.jpg)
double percentage(long nvotes, long total) { return nvotes*100.0/total;}
![Page 62: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/62.jpg)
taxi
![Page 63: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/63.jpg)
![Page 64: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/64.jpg)
unit_distance max_distance fare
Every 1000 m next 1 km $3.40
Every 400 m next 9.2 km $0.22
Every 350 m next ∞ km $0.22
![Page 65: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/65.jpg)
double fare = 0; for (int i = 0; i < NUM_TIERS; i += 1) { if (distance < 0) { return fare; } long min_dist = min(distance, tiers[i].max_distance); fare += tiers[i].fare * ceil(min_dist*1.0 / tiers[i].unit_distance); distance -= tiers[i].max_distance; }
![Page 66: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/66.jpg)
inversions
![Page 67: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/67.jpg)
left right
![Page 68: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/68.jpg)
Pattern Recognition
observe trends and patterns, then generalise
![Page 69: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/69.jpg)
Abstraction
![Page 70: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/70.jpg)
![Page 71: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/71.jpg)
![Page 72: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/72.jpg)
![Page 73: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/73.jpg)
![Page 74: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/74.jpg)
![Page 75: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/75.jpg)
5 50011000110000001104001100
![Page 76: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/76.jpg)
Who draw pictures like this??
![Page 77: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/77.jpg)
abstract generalize
solve
![Page 78: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/78.jpg)
![Page 79: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/79.jpg)
Abstraction
identifying and abstracting relevant information
generalize to other domains
![Page 80: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/80.jpg)
1. Decomposition 2. Pattern Recognition 3. Abstraction 4. Algorithms
![Page 81: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/81.jpg)
AY18/19 Sem 1
Some words of advice
![Page 82: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/82.jpg)
AY18/19 Sem 1
Work hard. Very very hard.
![Page 83: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/83.jpg)
what doesn’t kill you only makes you
stronger
![Page 84: Lecture 12 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010 … · Morning. Final 30% Midterm 15% PE 2 15% PE 1 10% Assignments 30%. Scope Everything from Unit 1 to Unit 28](https://reader033.vdocuments.us/reader033/viewer/2022051510/5fece5df12dcbc5f512e601a/html5/thumbnails/84.jpg)