Lecturers slides http www comp nus edu sgcs

  • Slides: 32
Download presentation
Lecturer’s slides http: //www. comp. nus. edu. sg/~cs 1010/ WEEK 7 Class Activities

Lecturer’s slides http: //www. comp. nus. edu. sg/~cs 1010/ WEEK 7 Class Activities

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 § Revision (or materials

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 § Revision (or materials that were earlier skipped due to lack of time) § Unit #7: Testing and Debugging § Taking Stock § Discussion of PE 1 § Surveys Week 7 - 2

© NUS CS 1010 (AY 2014/5 Semester 1) Testing and Debugging § We will

© NUS CS 1010 (AY 2014/5 Semester 1) Testing and Debugging § We will go through Unit #7 Testing and Debugging now Week 7 - 3

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 4 Taking Stock

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 4 Taking Stock § We are mid-way in the semester! § Let’s take stock of what you have learned so far

© NUS CS 1010 (AY 2014/5 Semester 1) Topics in C covered so far

© NUS CS 1010 (AY 2014/5 Semester 1) Topics in C covered so far § Control structures § Basic C program structure § Sequential statements § Selection statements § Relational operators (<, § main() function § Variable declarations § Data types (int, float, § § double, char) § Arithmetic operations (scanf(), printf()) § Preprocessor directives § #include § #define Logical operators (&&, ||, !) Conditional operator (? : ) Integer as boolean if, if-else, switch § Repetition statements § while, do-while, for (+, -, *, /, %) § Input/output functions <=, >, >=, ==, !=) § Functions § § Return type Parameters Function prototypes Scope of variables/parameters § Pointers § Arrays Week 7 - 5

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 6 Description and

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 6 Description and Objectives Introduces the fundamental concepts of problem solving by computing and programming using an imperative programming language. Outcomes Solve simple algorithmic problems Write good small programs C as a tool Not just about C

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 7 Skills Language

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 7 Skills Language constructs Problem solving Coding

© NUS CS 1010 (AY 2014/5 Semester 1) Topics in C covered so far

© NUS CS 1010 (AY 2014/5 Semester 1) Topics in C covered so far § Control structures § Basic C program structure § Sequential statements § Selection statements § Relational operators (<, § main() function § Variable declarations § Data types (int, float, § § double, char) § Arithmetic operations (scanf(), printf()) § Preprocessor directives § #include § #define Logical operators (&&, ||, !) Conditional operator (? : ) Integer as boolean if, if-else, switch § Repetition statements § while, do-while, for (+, -, *, /, %) § Input/output functions <=, >, >=, ==, !=) § Functions § § Return type Parameters Function prototypes Scope of variables/parameters § Pointers § Arrays Week 7 - 8

© NUS CS 1010 (AY 2014/5 Semester 1) Topics in C covered so far

© NUS CS 1010 (AY 2014/5 Semester 1) Topics in C covered so far §§ Control §Basic Control § C Control §CControl Basic C § Control §§Basic § Control § structures Control structures Basic C §program Basic C program structures §program Basic C structures §program Basic CC structures §program Basic program §program Basic C program §Sequential statements § §Sequential § Basic C program sstructure § Sequential § § Basic C program structure § Seque program § Basic C program structure §Sequent §§structure Basic C program structure §Sequential structure statements §statements Selection statements structure statements §§§structure main statements structure main() Sequ structure ntial §§ structure main() structure al statements § Relational operators (<, §main() structure statements §main() §() § main() function ential § Selection §functio main() <=, >, >=, ==, !=) § §Selection statem §main() function Selection stateme function § § Selection statemen § Selection § main() function functi § §Selection § Logical operators (&&, ||, !) function §Selection n. Variabl §function Variable statements §§state § Variable statements function ts § Variable declarations statements § Conditional operator (? : ) statements §Variable statements Variable § Variable statements ment § §§on statements §Variable declarations § Relational § Integer as boolean §§Selectio Data types (int, float, § Relational §edeclarati Variable § Relational declarations §Selecti § Relational § Varia Selection § Relational declarations § Relatio declaratio § if, if-else, switch sledeclara § Relation declarations § Relational double, char)(int, declaration operators § Data types on operators (<, (<, <=, declarations operators n §§Data types (int, ons ble operators statemen § types operators Data types (int, nal ns declar al § Repetition statements § Arithmetic operations § Data types operators § §Sele s. Data tions (<, <=, >, float, double, statem <=, >, >=, § types stateme decla Data <=, >, >=, ==, float, double, ts §>, >=, while, do-while, <=, >, >=, ==, !=) for (int, float, (+, -, *, /, %) operato Data ations float, double, operators (int, float, (<, <=, >, § Data types § §ction Data ents >=, ==, !=) char) ==, !=) (int, float, types § Relati §nts Input/output functions char) !=) rs (<, double, char) § ratio Data types (int, § Functions § Logical char) (<, <=, >, state double, char) >=, ==, !=) types (int, float, § Rel § Logical § Rela ns § Arithmetic (scanf(), printf()) (int, float, double, § Logical onal § Logical Return type (&&, ||, §§Arithmetic types <=, >, Logical §§float, § §Logical ==, operators Arithmetic (int, Arithmetic atio § double, § ment Data operators tiona double, operations (+, § Parameters char) operators operat (int, operators (&&, §char) Preprocessor >=, ==, operations (+, double, operations stypes operators (&&, !=) float, nal !) operations (+, operators operations (+, § Function prototypes l (&&, ||, !) char) § Arithmetic , *, /, %) ors (<, (&&, ||, !) float, !=) ||, !) char) § R directives , *, /, %) (+, -, *, /, %) § Logical ||, !) double, op, *, /, %) of §double, Arithmetic (&&, ||, !) § § Scope Conditional , *, /, %) oper § Conditional §§(int, Arithmeti <=, >, operations § Input/output § Conditional § Logical el§Input/output §era #include § Conditional variables/parameters §§Arithmetic Input/output operators char) § Conditional float, operations § Condition ators Input/output operator (? : ) § Input/output c operator (? >=, operato char) (+, -, *, /, %) § #define functions operator (? : ) at operation (? : ) tors functions § doubl Arithme (&&, ||, !) operator functions § Pointers operator (+, -, *, /, %) (<, al operator functions operatio § Integer as functions ==, !=) : ) § Arithm § Input/output io (scanf(), § Integer as rs (&&, s (+, (<, tic § Integer as as (scanf(), §printf()) Conditio e, (scanf(), <=, §etic Input/outpu §as Integer (? : )Integer ns (+, (scanf(), § Arrays § Logic boolean (scanf(), printf()) § n functions ||, !)nal , *, /, %) boolean printf()) <=, operati char) printf()) boolean >, t functions § Integer as boolean , *, /, %) operat al printf()) § Preprocessor al boolean § if, if-else, switch (scanf(), § Conditi Input/outp >, § if, if-else, § Preprocessor ons (+, § §Arith operator § if, if-else, >=, § Preprocessor § ions Input/out Preprocessor o(scanf(), operat boolean § if, if-else, Week 7 - 9

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 10 So, what

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 10 So, what else did you learn besides C? Topics in C Program development v v v v Writing pseudocodes Edit – compile – execute” cycle Step-wise refinement Hand-tracing codes Incremental coding Testing Debugging Programming environment/tools v Operating system: UNIX v Editor: vim v Debugger: gdb Problem solving v Class exercises v Practice exercises v Lab assignments

© NUS CS 1010 (AY 2014/5 Semester 1) What Lies Ahead? § Week 8:

© NUS CS 1010 (AY 2014/5 Semester 1) What Lies Ahead? § Week 8: (1) More about pointers – using address parameters in functions; (2) File Processing § Week 9: Characters and Strings § Week 10: Recursion § Week 11: Searching and Sorting § Week 12: Structures § (Note: the above order may be subject to change) Week 7 - 11

© NUS CS 1010 (AY 2014/5 Semester 1) Discussion of PE 1 § We

© NUS CS 1010 (AY 2014/5 Semester 1) Discussion of PE 1 § We will go through PE 1 questions now Week 7 - 12

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 13 Q 1.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 13 Q 1. Parking Fee: Task Statement (1/12) § Based on the following parking rate table, calculate the parking fee given day, time-in and time-out. § § Day: an integer between 1 (Monday) and 7 (Sunday) Time-in/time-out: an integer representing time in 24 -hr format. Weekday Saturday Sunday 4 am ~ 7 am $2. 00 / hr $2. 50 / hr $5. 00 / entry 7 am ~ 6 pm $1. 20 / 30 mins $1. 50 / 30 mins 6 pm ~ midnight $5. 00 / entry $7. 00 / entry § § Grace period: 10 minutes Surcharge for parking for more than 10 hours § § Weekdays: 10%, Saturday: 20%, Sunday: 0% Additional fee for exiting after 10 pm on any day: $3. 00 § Surcharge is not applicable on this fee.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 14 Q 1.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 14 Q 1. Parking Fee: Examples (2/12) § Example 1: Tuesday, 4: 29 am to 7: 50 am. § 4: 29 am to 7 am 3 * 1 -hour slots: $2. 00 * 3 = $6. 00 § 7 am to 7: 50 am 2 * 30 -minute slots: $1. 20 * 2 = $2. 40 § Total fee = $6. 00 + $2. 40 = $8. 40 4 am ~ 7 am ~ 6 pm ~ midnight Weekday $2. 00 / hr $1. 20 / 30 mins $5. 00 / entry Saturday Sunday $2. 50 / hr $5. 00 / entry $1. 50 / 30 mins $7. 00 / entry

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 15 Q 1.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 15 Q 1. Parking Fee: Examples (3/12) § Example 2: Saturday, 7: 01 am to 7: 49 pm. § 7: 01 am to 6 pm 22 * 30 -minute slots: $1. 50 * 22 = $33. 00 § 6 pm to 7: 49 pm one entry: $7. 00 § Surcharge: ($33. 00 + $7. 00) * 20% = $8. 00 § Total fee = $33. 00 + $7. 00 + $8. 00 = $48. 00 4 am ~ 7 am ~ 6 pm ~ midnight § Weekday $2. 00 / hr $1. 20 / 30 mins $5. 00 / entry Saturday Sunday $2. 50 / hr $5. 00 / entry $1. 50 / 30 mins $7. 00 / entry Surcharge for parking for more than 10 hours § Weekdays: 10%, Saturday: 20%, Sunday: 0%

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 16 Q 1.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 16 Q 1. Parking Fee: Examples (4/12) § Example 3: Sunday, 3 pm to 10: 01 pm. § 3 pm to 10: 01 pm one entry: $5. 00 § Additional fee: $3. 00 § Total fee = $5. 00 + $3. 00 = $8. 00 4 am ~ 7 am ~ 6 pm ~ midnight § Weekday $2. 00 / hr $1. 20 / 30 mins $5. 00 / entry Saturday Sunday $2. 50 / hr $5. 00 / entry $1. 50 / 30 mins $7. 00 / entry Additional fee for exiting after 10 pm on any day: $3. 00

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 17 Q 1.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 17 Q 1. Parking Fee: Examples (5/12) § Example 4: Thursday, 11: 49 pm to 11: 59 pm. § Grace period: no charge § Total fee = $0. 00 4 am ~ 7 am ~ 6 pm ~ midnight § Weekday $2. 00 / hr $1. 20 / 30 mins $5. 00 / entry Grace period: 10 minutes Saturday Sunday $2. 50 / hr $5. 00 / entry $1. 50 / 30 mins $7. 00 / entry

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 18 Q 1.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 18 Q 1. Parking Fee: Examples (6/12) § Example 5: Monday, 12 pm to 10: 01 pm. § § § 12 pm to 6 pm 12 * 30 -minute slots: $1. 20 * 12 = $14. 40 6 pm to 10: 01 pm one entry: $5. 00 Surcharge: ($14. 40 + $5. 00) * 10% = $1. 94 Additional fee: $3. 00 Total fee = $14. 40 + $5. 00 + $1. 94 + $3. 00 = $24. 34 4 am ~ 7 am ~ 6 pm ~ midnight § Saturday Sunday $2. 50 / hr $5. 00 / entry $1. 50 / 30 mins $7. 00 / entry Surcharge for parking for more than 10 hours § § Weekday $2. 00 / hr $1. 20 / 30 mins $5. 00 / entry Weekdays: 10%, Saturday: 20%, Sunday: 0% Additional fee for exiting after 10 pm on any day: $3. 00

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 19 Q 1.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 19 Q 1. Parking Fee: Grace Period (7/12) § This problem is similar to the Taxi Fare problem (Practice Exercise #6) § As described in that exercise, time-in (time. In) should be converted to minutes since 0: 00, using the formula: § § § time. In = time. In / 100 * 60 + time. In % 100 We can do the same for time-out (time. Out) After the conversion, it is straightforward to check for the grace period § if (time. Out – time. In <= 10) total. Fee 0

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 20 Q 1.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 20 Q 1. Parking Fee: Base Fee (8/12) § Otherwise, total. Fee = base. Fee + surcharge (if any) + additional. Fee (if any) § base. Fee if (day == 7) base. Fee 5. 00 // Flat rate for Sunday else base. Fee period 1 Fee + period 2 Fee + period 3 Fee § period 1: 4 am to 7 am § period 2: 7 am to 6 pm § period 3: 6 pm to midnight

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 21 Q 1.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 21 Q 1. Parking Fee: 4 – 7 am (9/12) § period 1 Fee (4 am to 7 am) If time. Out is later than 7 am, we take 7 am as the cut-off for calculating period 1 Fee. if (time. In < 420) if (day == 6) period 1 Fee ceil ( (min(time. Out, 420) – time. In) / 60) * 2. 50 // Saturday else period 1 Fee ceil ( (min(time. Out, 420) – time. In) / 60) * 2. 00 // Weekdays else period 1 Fee 0 ceil rounds up the calculated number of 60 -min slots to an integer. § If time. In is before 7 am, period 1 Fee is charged based on day and the number of 60 -minute slots chargeable. § Here 420 is converted from 7 am (i. e. , 700 in 24 -hr format).

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 22 Q 1.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 22 Q 1. Parking Fee: 7 am – 6 pm (10/12) § period 2 Fee (7 am to 6 pm) We take 6 pm as the cut-off for ending point and 7 am as the cutoff for the starting point for calculating period 2 Fee if (time. In <= 1440 && time. Out >= 420) if (day == 6) period 2 Fee ceil ( (min(Time. Out, 1440) - max(time. In, 420) ) / 30) * 1. 50 // Saturday else period 2 Fee ceil ( (min(Time. Out, 1440) - max(time. In, 420) ) / 30) * 1. 20 // Weekdays else period 2 Fee 0 § If time. In is before 6 pm and time. Out is after 7 am, period 2 Fee is charged based on day and the number of 30 -minute slots chargeable. § Here 420 is converted from 7 am (i. e. , 700 in 24 -hr format), while 1440 is converted from 6 pm (i. e. , 1800 in 24 -hr format).

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 23 Q 1.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 23 Q 1. Parking Fee: 6 pm – midnight (11/12) § period 3 Fee (6 pm to midnight) if (time. Out > 1440) if (day == 6) period 3 Fee 7. 00 // Saturday else period 3 Fee 5. 00 // Weekdays else period 3 Fee 0 § If time. Out is after 6 pm, a per-entry fee is charged based on day. § Here 1440 is converted from 6 pm (i. e. , 1800 in 24 -hr format).

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 24 Q 1.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 24 Q 1. Parking Fee: Surchage/Additional Fee (12/12) § surcharge if (day >= 1 && day <= 5) surcharge base. Fee * 0. 1 // Weekday else if (day == 6) surcharge base. Fee * 0. 2 // Saturday else surcharge 0 // Sunday § additional. Fee if (time. Out > 1320) additional. Fee 3. 00 § Here 1320 is converted from 10 pm (i. e. , 2200 in 24 -hr format).

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 25 Q 2.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 25 Q 2. Happy Numbers: Task Statement (1/5) § For a positive integer S, if we sum up the squares of all digits in S, we get another (possibly different) integer S 1. § We can always repeat this process to get more integers. § Example: 3 3*3 = 9 9*9 = 81 8*8 + 1*1 = 65 … § The integers generated in this way always eventually reach one of these 10 numbers: § 0, 1, 4, 16, 20, 37, 42, 58, 89, or 145.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 26 Q 2.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 26 Q 2. Happy Numbers: Task Statement (2/5) § A positive integer S is said to be happy if one of the integers generated this way is 1. § § Examples: § 7 is a happy number since 7 7*7 = 49 4*4 + 9*9 = 97 9*9 + 7 *7 = 130 1*1 + 3*3 + 0*0 = 10 1*1 + 0*0 1 (which is one of the 10 terminating numbers) § 5 is not a happy number since 5 5*5 = 25 2*2 + 5*5 = 29 2*2 + 9*9 = 85 8*8 + 5*5 = 89 (which is one of the 10 terminating numbers) Given two ranges, compute and compare the number of happy numbers in them.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 27 Q 2.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 27 Q 2. Happy Numbers (3/5) § This problem is similar to the square-free numbers exercise (Lab #2 Ex 2) § The only difference is that now we are checking for happy numbers instead of square-free integers. § Therefore, all you need is to replace is_square_free() in your program for Lab #2 Ex 2 with is. Happy. Number()

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 28 Q 2.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 28 Q 2. Happy Numbers (4/5) § How to write is. Happy. Number()? § Key observations: If one of the numbers generated is… The numbers generated afterwards are… 0 0 1 1 4, 16, 20, 37, 42, 58, 89, 145 § We can stop generating numbers once the generated number is one of 0, 1, 4, 16, 20, 37, 42, 58, 89 or 145. § If the generated number is 1, the original number is happy, otherwise it is not.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 29 Q 2.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 29 Q 2. Happy Numbers (5/5) § Algorithm is. Happy. Number(num){ while (num is not 0, 1, 4, 16, 20, 37, 42, 58, 89 or 145) num sum. Of. Digit. Squares(num) return (num == 1) sum. Of. Digit. Squares(num) { } sum 0 while (num > 0) { sum + (num % 10)*(num % 10) num / 10 } return sum }

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 30 Surveys §

© NUS CS 1010 (AY 2014/5 Semester 1) Week 7 - 30 Surveys § Please do the IVLE surveys now § § Very important for us to know your needs and how you are doing There are 3 surveys 1. For all CS 1010 students – everybody must do 2. Only for those who have attended the “Introduction to Programming” workshop in July 3. Only for those who have attended the “Refresher Workshop for Polytechnic Graduates” workshop in July

© NUS CS 1010 (AY 2014/5 Semester 1) Things-To-Do n Deadline for Lab #3

© NUS CS 1010 (AY 2014/5 Semester 1) Things-To-Do n Deadline for Lab #3 n n Deadline: 4 October 2014, Saturday, 9 am Continue to do practice exercises on Code. Crunch Week 7 - 31

© NUS CS 1010 (AY 2014/5 Semester 1) End of File Week 7 -

© NUS CS 1010 (AY 2014/5 Semester 1) End of File Week 7 - 32