Week 6 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 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 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. 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 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 {…} 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) 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 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 //some function description here… int count_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; } 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 // This function computes the total number of candles burned. int count_candles (int n, int k) { int candles_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; } int count_candles(int candles, int residuals) { int actual_residuals = candles; // initial number of residuals int new_candles; while (actual_residuals >= residuals) { new_candles = actual_residuals/residuals; candles += new_candles; actual_residuals = actual_residuals%residuals + new_candles; } return candles; } Inefficiency if you write this int count_candles (int n, int k) { int i = n; // Initial number of candles int count = 0; // Number of new candles while (n >= k) // Pre-condition, execution stops when n < k { n -= (k - 1); // After each set of k candles are burnt count ++; // 1 new candle can be rolled out } return i + count; // Total numbers of candles equals to initial number // of candles plus new candles created. } 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. Some of you uses only one while loop (did not consider the case when yielded-sum is not a single digit again) int sum_digits (int number) { while (number >= 10) { number %= 10; number += number; } return number; } Some of you did not consider the case when the input integer is already a single digit. int sum_digits(int number) { int total; int temp; while(number >= 10) { total = 0; while(number != 0) { temp = number % 10; total += temp; number /= 10; } number = total; } return total; } int sum_digits (int number) { int digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8, digit9, digit10; Some of you write algorithm which is not general while (number >= 10) { digit1 = number / 1000000000; digit2 = (number % 1000000000) / 100000000; digit3 = (number % 100000000) / 10000000; digit4 = (number % 10000000) / 1000000; digit5 = (number % 1000000) / 100000; digit6 = (number % 100000) / 10000; digit7 = (number % 10000) / 1000; digit8 = (number % 1000) / 100; digit9 = (number % 100) /10; digit10 = (number % 10); number = digit1 + digit2 + digit3 + digit4 + digit5 + digit6 + digit7 + digit8 + digit9 +digit10; } return number; } Read your Lab 1 and Lab 2 feedback Understand all lecture notes covered Do some hands-on practice Good sleep before that day