190 likes | 304 Views
CS1010E Programming Methodology Tutorial 1 Basic Data Type and Input/output, Characters and Problem Solving. C14,A15,D11,C08,C11,A02. Introduction: Myself. My name: Fan Qi Year 2 PhD student Haven’t code C for years Some details are forgotten Experience in teach C for years as well
E N D
CS1010E Programming MethodologyTutorial 1Basic Data Type and Input/output, Characters and Problem Solving C14,A15,D11,C08,C11,A02
Introduction: Myself • My name: Fan Qi • Year 2 PhD student • Haven’t code C for years • Some details are forgotten • Experience in teach C for years as well • CS1101 (c), CS1010 (c), CS1010E (c), CS3223 … • Email: fan.qi@comp.nus.edu.sg • I usually reply fast... unless I’m AFK
Tutorials • No marks for attendance or participation • If you come, do prepare !! Or you will be lost !! • Feel free to ask! • You already paid tuition fee, I won’t charge you any more • Time is limited: 45mins only I’ll focus on some questions and we can discuss further after class • Google is your best friend! • Most of your doubts are Googlable
Question: what if I want output “I love\n programming” into console? “\n \r \b \t” … are called Escape Sequence Google them for more information
Try input: 123, 123.3, 1150869504, can you explain ? What if you change last statement to be: printf(“Double Value = %d\n”, doubleValue); Can you explain ?
Try : printf(“%d, %d\n”, x, y); What will you see?
Answer: The output is 3.000000. In the above case, because x and y are both int, x/y gives and intvalue 3. 1. float z = 1.0 * x / y; 2. float z = (float) x / y; 3. float z = 1.0 * (x / y); 4. float z = x / y * 1.0; 5. float z = x / (float)y ; Which ones will output 3.500000?
Notice the data type What if you use: int digit1, digit2, digit3
Question 3 Least Common Multiplier • Analysis (Undestanding the problem) • Design (Devising a plan) • Implementation (Carrying out a plan) • Testing (Looking back)
Analysis • The given data are two numbers. Let them be x and y. The unknown we need to find is LCM(x, y) • We can assume that LCM of 2 numbers is always positive. In that case, we can also assume integer values x > y ≥ 0 • Other cases of x and y can be easily treated by using their absolute values.
Design • Another related problem we have seen is finding GCD of two numbers. • As LCM(x, y) = x*y/GCD(x, y), we can break the current problem into two smaller steps: • Finding GCD(x, y) • Computing x*y/GCD(x, y)
Implementation As the solution can be split into 2 smaller steps, we can now implement it bycarrying out each step: Step 1: Finding GCD(x, y) • 1.1. If y = 0, then the GCD is x and algorithm ends. • 1.2. Otherwise, GCD(x, y) = GCD(y, x%y) Step 2: Finding LCM(x, y) • Assign LCM(x, y) = x*y/GCD(x, y)
Testing • We can check the solution by assigning different values to x and y, such as (x = 0, y = 0); (x = 0, y ≠ 0); (x ≠ 0, y ≠ 0); (x = y); (x < y); (x > y); etc... • Corner cases! • For any problem, it is always important to check various situations. All test cases need to give correct results.
Is this algorithm... • LCM(x, y) = x*y/GCD(x, y) How to deal with overflow problem ? • Exact • Is every step deterministic? Same input->same output • Terminating • Will it run forever? • Efficient & Effective • Is the result correct? Is the running time acceptable? • General • Does it work for all valid inputs?
Thank you ! • See you next week!