Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1010E Programming Methodology Tutorial 1 Basic Data Type and Input/output, Characters and Problem Solving C14,A15,D11,C08,C11,A02.

Similar presentations


Presentation on theme: "CS1010E Programming Methodology Tutorial 1 Basic Data Type and Input/output, Characters and Problem Solving C14,A15,D11,C08,C11,A02."— Presentation transcript:

1 CS1010E Programming Methodology Tutorial 1 Basic Data Type and Input/output, Characters and Problem Solving C14,A15,D11,C08,C11,A02

2 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 … I usually reply fast... unless I’m AFK

3 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 

4 Question 1:

5 “\n \r \b \t” … are called Escape Sequence
Printf(“I love\\nprogramming”); “\n \r \b \t” … are called Escape Sequence Google them for more information  Question: what if I want output “I love\n programming” into console?

6 Try input: 123, 123.3, 1150869504, can you explain ?
Google IEEE floating point standard! It will output the integer part of the input 123->123 123.3->123 Try input: 123, , , can you explain ? What if you change last statement to be: printf(“Double Value = %d\n”, doubleValue); Can you explain ?

7 Try : printf(“%d, %d\n”, x, y); What will you see?
When y = x; there is a coercion When printf(“%d”, x); there is no coercion. Therefore you will see the output is , -2 Try : printf(“%d, %d\n”, x, y); What will you see?

8 Answer: The output is In the above case, because x and y are both int, x/y gives and int value 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 ?

9

10 Notice the data type What if you use: int digit1, digit2, digit3

11

12 Question: how do you get ASCII table search online?
Int i = whaterver int < 128; Printf(“%d=%c”, i, i);

13 Question 3 Least Common Multiplier Analysis (Undestanding the problem)
Design (Devising a plan) Implementation (Carrying out a plan) Testing (Looking back)

14 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.

15 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)

16 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)

17 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.

18 Is this algorithm... LCM(x, y) = x*y/GCD(x, y) 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? LCM(x, y) = x*y/GCD(x, y) Exact! Terminate! Correct, acceptable Not all! There is an overflow on x * y Overflow always exists, x*y encounter overflow very early! int a =x, b = y; While(a != b) { while( a< b) a+=x; while( a>b) b+= y; } LCM(x,y) = x / GCD(x,y) * y; ? How to deal with overflow problem ?

19 Thank you ! See you next week! 


Download ppt "CS1010E Programming Methodology Tutorial 1 Basic Data Type and Input/output, Characters and Problem Solving C14,A15,D11,C08,C11,A02."

Similar presentations


Ads by Google