class: middle, center # CS1010 ## Programming Methodology
--- class: middle, center # Lecture 1 ### 9 August 2021 Admin Matters
Unit 1: **What is a Program?**
Unit 2: **Computational Problems and Algorithms**
--- class: middle, center # Ooi Wei Tsang ### ooiwt@comp.nus.edu.sg ### Office Hour : Tuesday 4 - 5 PM --- class: middle, center ## What will you learn? --- class: middle - Comfortable with reading and developing small programs to solve a given problem - Become proficient with C and associated programming tools (`vim`, `clang`, etc) --- class: middle ## What do you need to know before CS1010? --- class: middle - Comfortable with using a computer - Comfortable with secondary school mathematics and notations - Nothing about programming --- class: middle,center ## Weekly Activities --- class: middle,wide Day | Activities :------------------|:-------------------------- Mon | Attend lecture Mon - Wed | Review; problem set; quiz Wed | Attend tutorial Thu | Attend lab Thu - Tue | Programming assignment; exercsies --- class: middle ### Tutorial and lab sessions - start in Week **3** - online options for those on LOA / overseas --- ### Tutorial - problem set will be discussed - 20 students per group - lead by a student tutor --- ### Lab - programming assignment will be discussed - hands-on practice - 10 students per group - lead by a student tutor --- class: middle ### Lab/Tutorial Registration - Please manage through ModReg - If you need to talk to a human, check with - Enzio Kam [`dcsekhh@nus.edu.sg`](mailto:dcsekhh@nus.edu.sg) - Foo Yong Qi [`dcsyqf@nus.edu.sg`](mailto:dcsyqf@nus.edu.sg) - Module instructors do not have access to the system --- class: middle ### Online Lab/Tutorial Policy - Online attendance applies to students stranded overseas/SHN/QO/LOA - Online tutorial: Wednesday 6 - 7 PM - Online lab: Thursday 6 - 8 PM --- class: middle,wide - **Category I**: Cannot attend F2F session in Weeks 3-4 only - Preallocated: no action needed - Not Preallocated: bid for a F2F slot as usual - Attend online version during Week 3-4 - **Category II**: Cannot attend F2F session Weeks 3-5 (at least) - Preallocated: drop pre-allocated slot - Not Preallocated: nothing to drop - Attend online version for the rest of the semester --- class: middle ### [Register as Online Student Here](https://luminus.nus.edu.sg/modules/600de8e9-67ec-47dd-b597-a2eeadb45792/survey/16f85196-1749-4638-9cb9-82f111c1a449) Only for those with valid reasons (e.g., cannot enter Singapore at all or early enough) --- class: middle ## "Catch-Up" Sessions - About every three weeks - Saturday 10 AM - 12 PM, online - Q&A format --- class: middle, center ## Important Resources --- class: middle, center ## Module Homepage [`https://nus-cs1010.github.io/2122-s1/`](https://nus-cs1010.github.io/2122-s1/) lecture notes
problem sets
exercises
assignments
guides and manuals
readings
--- class: middle, center, wide ## Piazza [`https://piazza.com/nus.edu.sg/fall2021/cs1010/home`](https://piazza.com/nus.edu.sg/fall2021/cs1010/home) online forum
announcements
contact the teaching team
--- class: middle, center ## GitHub assignment submissions
assignment grading
--- class: middle, center ## LumiNUS quizzes
grades
survey
etc. --- class: middle, center # Assessments (or, how you will be graded) --- class:top,center
--- class:middle,center .smaller[ Assessment | Date | Time :------------------|:-------------------------- |:------------- Midterm | Mon, 27 Sep| 12 - 2 PM Final | Tue, 25 Nov| 5 - 7 PM Practical Exam 1 | Sat, 2 Oct| 9 AM - 12 PM Practical Exam 2 | Sat, 6 Nov| 9 AM - 12 PM ] --- class:middle,center ## Open Book Exam ### Bring any (analog) materials you want ### Nothing to memorize --- class:middle ## Not "bell-curved" - Your grade is based solely on your marks - Not relative to others in the class. --- class: middle ## Plagiarism and Cheating - "No mercy" policy in dealing with cases - Not worth it to cheat/copy --- class: middle ## Things To Do - Read about other policies - Get your accounts ready --- class: middle ## Office Hour Tomorrow If you have issues with your login, pop by my office hours (AS6 05-14 or [Zoom](https://piazza.com/class/krfo8m0yrkv6t2?cid=8)) --- class: center,middle # Words of Advice --- class: center,middle ## "Difficult" ## "High Workload" (but also "fun", "learned a lot") --- class: center,middle ## Expected workload ## 10 hours/week But your mileage may vary --- class: center,middle ## Learning in NUS SoC is different from your experience --- class: center,middle ### Less rote learning ### More open-ended problems ### A lot more self-learning --- class: center,middle,wide ### Cannot "study" programming / problem solving ### Need to actually _do it_ --- class: middle ### _Don't_: Stop at understanding the notes ### _Do_: "play" with it, ask "what if..", "why..", "why not..", etc --- class: center,middle ### _Don't:_ do it alone ### _Do:_ Discuss with classmates and teaching team --- class: center,middle ### _Don't:_ Optimize for CAP and grades ### _Do:_ Optimize for skills, knowledge, and experience --- class: center,middle ### _Don't:_ Do enough to get things done ### _Do:_ Invest time to master the tools (e.g., `bash`, `vim`, `git`, etc) --- class: center,middle,wide ### _Don't:_ focus on meeting assignment deadline ### _Do:_ think long term (your career) --- class: middle - No matter how well you have done to get here - No matter what you know about programming ### This is a fresh start --- class: middle, center # Lecture 1 ### 9 August 2021 Admin Matters
Unit 1: **What is a Program?**
Unit 2: **Computational Problems and Algorithms**
--- class: middle,center ## What is a program? --- class: middle,center ### A sequence of step-by-step instructions given to computers to process data in order to perform a certain task. --- class: middle,center ### How CPU Executes a Program .fit[![simplified](figures/simplified-cpu-mem-dark.png)] .tiny[(A highly simplfied view)] --- ## Machine Code - Instructions that can be interpreted by a CPU - Different CPU architecture may use different instruction sets `1001010001011110100010101010101000101` --- ## Assembly Language - Use 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. --- ## Higher-Level Language - Allow programmers to express an operation closer to their intention - E.g., $x = x + y$ if $y < 0$ --- ## Higher-Level Languages - FORTRAN (first popular programming language from 1950s) - C (1972, still influential today) - Many many others --- ## Compilation A compiler _compiles_ a program written in high-level language to machine code. .fit[![simplified](figures/c-compile-run-dark/c-compile-run-dark.001.png)] --- class: middle,center # What you will learn? --- class: middle,center ### How a C program behaves --- class: middle,center ### How to write C programs --- class: middle,center ### Tools and techniques to write good and correct C programs --- class: middle,center,wide ### How to use C to solve computational problems --- class: middle ### Easy: write a program that does what you want ### Difficult: Knowing what should your program do --- class: middle,center ## What is a computational problem? --- class: middle ### Computational problems are - problems that can be solved _step-by-step_ by a computer - have _well-defined_ inputs, outputs, and constraints --- class: middle,center ## Is 1933091 prime? --- class: middle,center ## Is there a route from a Point A to Point B? --- class: middle,center ## Find a route from Point A to Point B. (search problem) --- class: middle,center ## How many different routes are there from Point A to Point B? (counting problem) --- class: middle,center ## Find the shortest route from Point A to Point B. (optimization problem) --- class: middle,center ## What is the meaning of life? --- class: middle,center ## Do I look good in this outfit? --- class: middle ### Computational problem are - problems that can be solved _step-by-step_ by a computer - have _well-defined_ inputs, outputs, and constraints --- class: middle,center ### Find the maximum among these numbers ``` 5 9 8 1 3 2 ``` --- class: middle,center ### Find the maximum among these numbers .tiny[ ``` 12 20 11 20 14 2 24 36 43 16 27 6 11 7 10 15 38 10 22 7 16 26 32 30 11 9 30 6 20 6 27 19 26 10 27 6 19 34 5 9 3 22 10 4 13 1 10 22 43 36 39 29 41 12 13 25 17 8 30 31 29 38 2 42 7 45 24 33 9 40 34 29 37 2 26 17 19 34 6 7 34 22 21 41 38 5 15 13 9 1 42 39 5 29 38 4 22 29 41 10 26 32 30 26 16 16 18 22 32 34 14 10 5 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 . ``` ] --- class: middle ### 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. --- class: middle ### Problem Formulation - Input: $L$ = $\langle l\_0,...,l\_{k-1\} \rangle$ - Output: $m$ - Constraint $m \in L$ and $m \ge l_i$ for all $i$. --- class: middle ### Scan One-By-One - $l_i$ : The number being scanned - $i = 0, 1, \ldots k-1$ --- class: middle ### "Maximum So Far" - $m$ : max so far - $m = l_0$ initially --- class: middle ### Keeping track - Compare $m$ with $l\_i$ - if $m < l\_i$, then update $m$ with the value of $l\_i$. --- class: middle ### Done scanning? - When we have scanned $l\_{k-1}$ --- class: middle ### The steps to solve a computational problem form the _algorithm_ --- class: middle ## Flowchart .fit[![flowchart](figures/max-flowchart-dark/max-flowchart-dark.001.png)] --- class:center ![:scale 75%](figures/max-flowchart-dark/max-flowchart-dark.001.png) .smaller.center[ $k$ | $i$ | $l\_i$ | $m$ :---|:----|--------|----- | | | | | | | | | | | | ] --- ### Variables - $i$, $m$, $k$ are _variables_ - They have corresponding location in the memory and hold a _value_ - You can _read_ a value from a variable - You can _write_ a value to a variable --- ### 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 $l_i$). --- ### Comparison - We can compare the values of two variables - E.g., $i$ equals $k$? $l\_i > m$? --- ### Arithmetic Operations - We can perform arithmetic operations - E.g., we set $i$ to $i + 1$ --- class: middle ### Does this algorithm always correctly find the maximum value? .fit[![flowchart](figures/max-flowchart-dark/max-flowchart-dark.001.png)] --- class: middle ### Correctness - 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! --- class: middle ### How to check for correctness? - Carefully choose a set of test cases to try - Argue formally about the properties of the algorithms --- class: middle ### Homework - Post-Lecture Diagnostic Quiz - Due Wednesday 2359 - You have unlimited attempts - Problem Set 1.1 to 1.3 - Not graded. To be discussed Week 3. - Read the policies - Setup accounts