cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang...
Transcript of cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang...
![Page 1: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/1.jpg)
![Page 2: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/2.jpg)
AY18/19 Sem 1
CS1010Programming Methodology
![Page 3: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/3.jpg)
Lecture 1
Admin MattersUnit 1: What is a Program?
Unit 2: Computational Problems & Algorithms
14 August 2018
![Page 5: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/5.jpg)
AY18/19 Sem 1
What will you learn?
![Page 6: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/6.jpg)
AY18/19 Sem 1
• Comfortable with reading and developing small programs to solve a given problem
• Become proficient with C syntax and language and associated programming tools (editors, debuggers)
![Page 7: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/7.jpg)
What do you need to know to be here?
• Nothing about writing programs
• Comfortable with using a computer
• Comfortable with secondary school math and notations
![Page 8: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/8.jpg)
Weekly Activities
• 2-hour lecture: Tues 4-6pm, weekly
• 2-hour tutorial / lab: up to 15 students per lab, starting Week 3
![Page 9: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/9.jpg)
UDLs and TAs
• UDLs: Your seniors who return to help your learn in CS1010
• TAs: PhD students who teach part-time / full-time
![Page 10: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/10.jpg)
AY18/19 Sem 1
Open Book Exams
Not a memorisation-based module
Any APIs needed will be given
![Page 11: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/11.jpg)
AY18/19 Sem 1
Module Homepage
https://nus-cs1010.github.io/1819-s1/
lecture notesassignments
guides/manualsetc.
![Page 12: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/12.jpg)
AY18/19 Sem 1
Piazza Q&A
https://piazza.com/class/jkqlna92ju045j
![Page 13: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/13.jpg)
AY18/19 Sem 1
Important Dates
October 2, 2018 (Midterm)
November 27, 2018 (Final)
![Page 14: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/14.jpg)
AY18/19 Sem 1
Important Dates (tentative)
October 6, 2018 (PE1)
November 10, 2018 (PE2)
![Page 15: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/15.jpg)
AY18/19 Sem 1
Open Book
Bring any materials you wish
Nothing to memorise
![Page 16: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/16.jpg)
Final30%
Midterm15%
PE 215%
PE 110%
Assignments30%
![Page 17: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/17.jpg)
CS1010 is not graded on a bell curve
0
10
20
30
40
A+ A to A- B+ to B B- to C D+ to D F
NOT the actual NUS bell curve
![Page 18: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/18.jpg)
In CS1010, you will get the grade you deserved, irrespective of other students in the class.
![Page 19: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/19.jpg)
AY18/19 Sem 1
Strict policy on plagiarism
Disciplinary action will be taken :(
![Page 20: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/20.jpg)
AY18/19 Sem 1
Accounts• Register for an SoC UNIX account if you do not have one
at https://mysoc.nus.edu.sg/~newacct/
• Register for a GitHub account if you do not have one at https://www.github.com/
• Activate your Piazza account (link emailed to you)
![Page 21: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/21.jpg)
AY18/19 Sem 1
Policies• Read about other CS1010 policies at the module website
• Using email and Piazza
• Attendance
• Late submission
• Slides / screencast availability
• …
![Page 22: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/22.jpg)
AY18/19 Sem 1
Some words of advice
![Page 23: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/23.jpg)
AY18/19 Sem 1
Work hard. Very very hard.
![Page 24: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/24.jpg)
At NUS SoC, learning is very different from your
prior education
![Page 25: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/25.jpg)
less rote learning;more open-ended problems;
more self-learning
![Page 26: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/26.jpg)
don’t optimise for grades and CAPs
![Page 27: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/27.jpg)
optimise your skills, knowledge and
experienceinstead
![Page 28: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/28.jpg)
invest time to master the tools
(e.g., bash, vim, git, etc)think long term benefits,
not how to get things done now
![Page 29: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/29.jpg)
What is a program? What is a computational problem?
What is an algorithm?
![Page 30: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/30.jpg)
What is a program?
![Page 31: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/31.jpg)
AY18/19 Sem 1
A Simplified View of a Computer
Central Processing
Unit
(CPU)
Memory
10100101
01001001
dataor
instructions
data
![Page 32: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/32.jpg)
Machine Code• Instructions that can be interpreted
by a CPU
• Different CPU architecture may use different instruction sets
1001010001011110101001010101010100101…
![Page 33: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/33.jpg)
Assembly Language
• Uses mnemonic to represent the instructions such as incr, decr, store, etc., in a more human readable way.
• Still difficult to write: operation that is conceptually simple still requires multiple instructions to be issued.
![Page 34: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/34.jpg)
Higher-Level Language• Languages that allow programmers
to express an operation closer to their intention
• e.g., x = x + y if (y < 0)
![Page 35: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/35.jpg)
Higher-Level Languages• FORTRAN (first popular PL, 1950s)
• C (1972, still influential today)
• Many many others
![Page 36: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/36.jpg)
AY18/19 Sem 1
Compiling High-Level Program to Machine Code
C program compile
(etc)
machinecode run
![Page 37: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/37.jpg)
AY18/19 Sem 1
What will you learn?
![Page 38: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/38.jpg)
AY18/19 Sem 1
How to write C
![Page 39: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/39.jpg)
AY18/19 Sem 1
How a C program behaves
![Page 40: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/40.jpg)
AY18/19 Sem 1
Tools and techniques to help write good and correct C programs
![Page 41: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/41.jpg)
AY18/19 Sem 1
How to use C to solve computational problems
![Page 42: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/42.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 43: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/43.jpg)
What is a computational problem?
![Page 44: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/44.jpg)
Computational Problems• Problems that can be solved step-by-
step by a computer
• Have well-defined inputs, outputs, and constraints
![Page 45: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/45.jpg)
Is 1933091 prime?
![Page 46: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/46.jpg)
Is there a route from a point A to point B?
![Page 47: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/47.jpg)
Find a route from a point A to point B
![Page 48: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/48.jpg)
How many different routes are there from a
point A to point B?
![Page 49: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/49.jpg)
Find the fastest route from a point A to point B
![Page 50: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/50.jpg)
What is the meaning of life?
![Page 51: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/51.jpg)
Do I look good in this outfit?
![Page 52: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/52.jpg)
Computational Problems• Problems that can be solved step-by-
step by a computer
• Have well-defined inputs, outputs, and constraints
![Page 53: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/53.jpg)
Find the maximum number
5 9 8
1 3 2
![Page 54: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/54.jpg)
Find the maximum12 20 11 20 14 2 24 36 43 16 27 6 11 710 15 38 10 22 7 16 26 32 30 11 9 30 620 6 27 19 26 10 27 6 19 34 5 9 3 2210 4 13 1 10 22 43 36 39 29 41 12 13 2517 8 30 31 29 38 2 42 7 45 24 33 9 4034 29 37 2 26 17 19 34 6 7 34 22 21 4138 5 15 13 9 1 42 39 5 29 38 4 22 2941 10 26 32 30 26 16 16 18 22 32 34 14 105 17 25 16 19 6 31 16 3 13 8 42 41 0
13 30 44 1 41 14 5 39 40 38 6 37 38 9
![Page 55: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/55.jpg)
How to Solve It?• Scan the integers one by one
• Keep track of the “max so far”
• When we are done scanning, the “max so far” will be the answer.
![Page 56: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/56.jpg)
Problem Formulation
• Input: L = {l0, l1, …, lk-1}
• Output: m
• Constraint: m ∈ L and m ≥ li for all i.
![Page 57: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/57.jpg)
“Scan one-by-one”
• li : the number being scanned
• i = 0, 1, … k-1
![Page 58: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/58.jpg)
“Maximum so far”
• m: max so far
• m = l0 initially
![Page 59: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/59.jpg)
Keeping Track..
• Compare m with li
• If m < li, then update m with value of li
![Page 60: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/60.jpg)
Done Scanning?
• When we have scanned lk-1
![Page 61: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/61.jpg)
The steps to solve a computational problem are called “algorithm”
![Page 62: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/62.jpg)
Flowchart
set m to l0 set i to 1
i equals k?
output m
YES
is li > m ? set m to li increment iYESNO
NO
input k and l0..lk-1
![Page 63: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/63.jpg)
set m to l0 set i to 1
i equals k?
output m
YES
is li > m ? set m to li increment iYESNO
NO
input k and l0..lk-1
Input: L = { 0, 2, 1}, k = 3
k i li m
3 1 2 0
3 2 1 2
3 3 ? 2
3
![Page 64: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/64.jpg)
Variables
• i, m, and k are called variables
• They are location in memory that holds a value
• You can read a value from a variable
• You can write a value to a variable
![Page 65: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/65.jpg)
Assignment
• We can assign the value of a variable to a constant (e.g., set i to 1)
• or to the value of another variable (e.g., set m to li)
![Page 66: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/66.jpg)
Comparison
• We can compare the values of two variables
• E.g., i equals k?, li > m?
![Page 67: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/67.jpg)
Arithmetic Operations
• We can perform arithmetic operations (+, -, etc)
• E.g., set i to i + 1
![Page 68: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/68.jpg)
Does this algorithm always find the maximum value correctly?
set m to l0 set i to 1
i equals k?
output m
YES
is li > m ? set m to li increment iYESNO
NO
input k and l0..lk-1
![Page 69: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/69.jpg)
Correctness of Algorithm
• If an algorithm produces the correct output for one instance of input, it is not necessary correct
• To be correct, it has to produce the correct output for all possible inputs!
![Page 70: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/70.jpg)
How to check for correctness?
• Carefully choose a set of test cases to try
• Argue formally about the properties of the algorithms
![Page 71: cs1010-lec1 - National University of Singapore · 2018. 8. 17. · AY18/19 Sem 1 Ooi Wei Tsang ooiwt@comp.nus.edu.sg AS6 05-15 CS1010 Office Hours: Wed 4-5pm](https://reader036.fdocuments.us/reader036/viewer/2022062606/6005ee5a550cc763fc094c6d/html5/thumbnails/71.jpg)
Homework
• Try out Problem Set 1.1 to 1.3
• We will discuss in Week 3 !