290 likes | 315 Views
Join the CS1010 Discussion Group for Week 3 to discuss computational thinking, algorithms, and programming. Complete tutorial questions, ask questions, and make friends!
E N D
CS1010 Discussion Group 11 Week 3 - Computational Thinking/Algorithms
HELLO! I am Yan Hwa Computer Science Year 3 leeyanhwa@u.nus.edu E0004806@u.nus.edu http://www.comp.nus.edu.sg/~yanhwa/
Expectations • Complete tutorial questions before class so we can discuss • Ask any questions during class or post on forum • Attempt tutorial questions before expecting an answer • Also learning from you • Make friends!
About you • Programming background? (if any) • Your expectations? • Your concerns?
Discussion sessions • Can arrive earlier to ask any questions • Attendance will be taken; 5% of final grade • Go through some questions in Discussion Sheet • QnA • After a few weeks, may split session into two sub-sessions • First session: Complusory • Second session: Experienced students may choose to leave
Reminders • Check IVLE regularly for announcements/forum postings • Participate actively in IVLE forums • Post in forums instead of sending emails • Have you tested out CodeCrunch by submitting theLab0? • Lab1has been released. Please take note the deadline is next Wednesday (6th September) 5pm. • Fill in “My Progress Chart” in the CS1010 Student Handbook regularly. I may spot-check!
Victory Road • Nature of CS1010 • This module can be very easy or very tough • Don’t leave it till the last minute • Not just a course on C programming • Practice make perfect!
Google is your best friend Let me google that for you
Problem Solving and Incremental Coding Inputs? Outputs? Constraints? Relationship? Lecture Summary Define Problem! Make Plan! Code, Test, Code, Test
Tips on Labs Do Follow Instructions Give meaningful names for variables Appropriate commenting Must do Double/triple check your indentation (gg=G in vim will auto-indent your program nicely!) Do not Forget to initialise variables whenever necessary Output format does not conform to requirement
Tips on Labs Ask early if unclear (Forums, Email) Do NOT copy from friends!(changing variable names, spacing, shifting codes around is not going to work!) Last-minute uploading could be a traumatic experience
CodeCrunch Lab #0 Have you submitted? Tutorial Exercise
Lab #1 Three Simple Exercises Read Ex3 Packing and fill in the worksheet Tutorial Exercise
Tutorial 1 Q4 Given a list of N integers, find out how many of them are negative. Let the list of integers be a1, a2, …, aN. for k from 1 to N if (a[k]< 0) countNeg countNeg + 1; print countNeg
Tutorial 1 Q4a Why must we initialise? countNeg0 for k from 1 to N if (a[k]< 0) countNeg countNeg + 1; print countNeg
Tutorial 1 Q4d How to use a for loop instead? sum 0; k 1; while ( k ≤ N ) { sum sum + a[k]; kk + 2; }
Find the number of swaps required to move all white balls to the left of black balls • Only swapping of neighbours allowed Tutorial 1 Q5 How many swaps needed? Not allowed!!
Find the number of swaps required to move all white balls to the left of black balls • Only swapping of neighbours allowed Tutorial 1 Q5 2 swaps?
Find the number of swaps required to move all white balls to the left of black balls • Only swapping of neighbours allowed Tutorial 1 Q5 2 swaps!
Tutorial 1 Q5 Warm-up exercise : Find the white balls! Similar to Q4? let’s call the balls Bk, 1 ≤ k≤ N where N is the number of balls countWhite 0; for k from 1 to N if (Bk is white) countWhite countWhite + 1; print countWhite
How to find out total number of swaps needed Decompose Problem Plan your solution Find Patterns
Tutorial 1 Q5 Algorithm 1 Number of swaps needed for each white ball = Position of white ball (k) – desired pos • countSwaps 0; • desiredPos 1; • for k from 1 to N • if (Bk is white) • numSwaps k – desiredPos; • countSwaps countSwaps + numSwaps; • desiredPos desiredPos + 1; • Print countSwaps
Tutorial 1 Q5 Algorithm 2 Number of swaps needed for each white ball = number of black balls to the left of the white ball • countSwaps 0; • blackToLeft 0; • For k from 1 to N • if (Bk is black) • blackToLeft blackToLeft + 1; • else • /*it is a white ball */ • countSwaps countSwaps + blackToLeft; • Print countSwaps
Tips for Vim Navigate around using arrow keys or hjkl Google for Vim Shortcuts and cheatsheets Use Vim Tutor!
Summary Computational thinking and Problem Solving Plan before programming anything Start doing your lab! Learn Vim! Practise!
Sources of Practice Reference book Practice exercises we put up on CodeCrunch (http://www.comp.nus.edu.sg/~cs1010/4_misc/practice.html) Google for more questions and cheatsheetshttps://courses.cs.washington.edu/courses/cse351/14sp/sections/1/Cheatsheet-c.pdf Books https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list
Sources of Practice https://blog.codinghorror.com/code-tells-you-how-comments-tell-you-why/