190 likes | 328 Views
Week 6. CS1010 Discussion Session Group 8. Outline. Lab 1 and 2 results Common mistakes in Style Lab 1 common mistakes in Design Lab 2 common mistakes in Design Tips on PE preparation Tutorial Questions for Week 6 Might not be able to cover all of them I’ll explain Question x, x, and x
E N D
Week 6 CS1010 Discussion Session Group 8
Outline • Lab 1 and 2 results • Common mistakes in Style • Lab 1 common mistakes in Design • Lab 2 common mistakes in Design • Tips on PE preparation • Tutorial Questions for Week 6 • Might not be able to cover all of them • I’ll explain Question x, x, and x • If you would like to explain another question, raise hands and I’ll explain
Lab 1 and Lab 2 Common Mistakes in Style • Discussion group? Name? Program description? (-1) • Function description (-1) • Did not write or write in weird places • Bad identifiers Name (-2) • If you want to use k, n, at least comment it somewhere to tell what they are • Superfluous comments (-1) • //executable statement
Lab 1 and Lab 2 Common Mistakes in Style • Indentation is wrong in all your programs • No deduction on marks this time • Try to utilize the auto-indentation feature in your vim editor • Better don’t use tab for indentation; it causes problem on Coursemarker.
Lab 1 General Result • One exercise is 10 marks. In total 30 marks. • 1 Attempt mark is given when you try all exercises • The majority of you are in the range 29~30
Lab 1 Common Mistakes in Design • Did not write functions at all, when the question explicitly asks to write some functions. (-5 for each omitted function) • Declare a function which return integer; but does not use the returned value in main function at all (-2) • Complex or redundant if else statements (-2) • If (x%2==0) {…} else if (x%2==1) {…} else {…}
Lab 1 Common Mistakes in Design • Output result in functions which are supposed to do computation only • Normally we do input and output in main function; and complex computation is carried out by functions. Unless the function is specifically designed to output (print_cookies)
Lab 2 General Result • Very Serious Mistakes • Missed the Deadline • 0 mark for everything…even the attempt mark. • Used Recursion (explicitly forbidden in question) • 0 mark for correctness and 0 mark for design • But since this is first time, so I’m more lenient (30 marks for correctness) • Did not test at all before submission (failed all test cases) • 0 mark for correctness • Even if some test cases happen to be true due to hard coding
Lab 2 Ex1: The 3x+1 Problem • Take any natural number n. If n is even, divide it by 2 to get n/2; if n is odd, triple it and add 1 to obtain 3n + 1. Repeat the process indefinitely. No matter what number your start with, you will always eventually reach 1. • While loop needed • n=1 is terminating condition
Lab 2 Ex1: The 3x+1 Problem • //some function description here… • intcount_iterations(int n) • { • int count = 0; // number of iterations • while (n > 1) • { • if (n%2 == 1) • n = 3*n + 1; • else • n /= 2; • count++; • } • return count; • }
Lab 2 Ex2: Candles • Alexandra has n candles. He burns them one at a time and carefully collects all unburnt residual wax. Out of the residual wax of exactly k (where k > 1) candles, he can roll out a new candle. • n/k gives new rolled out candles • n%k gives candles left
Lab 2 Ex2: Candles • // This function computes the total number of candles burned. • intcount_candles (int n, int k) • { • intcandles_burned = 0; • while (n >= k) // n is the total number of candles, k is the residual wax. • { • candles_burned = candles_burned + ((n/k) * k); • // candles burned is the total number of residual wax sets multiply • //the number of candles in that set. • n = (n/k) + (n%k); • // n is the number of new candles plus the previous remaining • //candles. • } • candles_burned = candles_burned + n; • return candles_burned; • }
Lab 2 Ex2: Candles • intcount_candles(int candles, int residuals) • { • intactual_residuals = candles; // initial number of residuals • intnew_candles; • while (actual_residuals >= residuals) • { • new_candles = actual_residuals/residuals; • candles += new_candles; • actual_residuals = actual_residuals%residuals + new_candles; • } • return candles; • }
Lab 2 Ex2: Candles • Inefficiency if you write this
Lab 2 Ex3: Fortune Cookies • Write a program cookies.c to read in a positive integer and add up its digits repeatedly until the sum is a single digit. For example, if the integer is 12345, then adding its digits (1 + 2 + 3 + 4 + 5) yields 15, and adding its digits again (1 + 5) yields 6. Hence the answer is 6.
Lab 2 Ex3: Fortune Cookies • Some of you uses only one while loop (did not consider the case when yielded-sum is not a single digit again)
Lab 2 Ex3: Fortune Cookies • Some of you did not consider the case when the input integer is already a single digit.
Lab 2 Ex3: Fortune Cookies • Some of you write algorithm which is not general
Prepare for your PE • Read your Lab 1 and Lab 2 feedback • Understand all lecture notes covered • Do some hands-on practice • Good sleep before that day