cs1010e programming methodology tutorial 1 basic data type and input/output, characters and problem...

19
CS1010E Programming Methodology Tutorial 1 Basic Data Type and Input/output, Characters and Problem Solving

Upload: abraham-kennedy

Post on 02-Jan-2016

219 views

Category:

Documents


3 download

TRANSCRIPT

CS1010E Programming MethodologyTutorial 1

Basic Data Type and Input/output, Characters and Problem Solving

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 …

• Email: [email protected]• 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

4

Question 1:

5

“\n \r \b \t” … are called Escape SequenceGoogle 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 ?

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?

8

Answer:The output is 3.000000. 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 3.500000?

9

10

Notice the data typeWhat if you use: int digit1, digit2, digit3

11

12

Question: how do you get ASCII table search online?

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:

1. Finding GCD(x, y)

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

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

How to deal with overflow problem ?

Thank you !

See you next week!