1 / 19

C14,A15,D11,C08,C11,A02

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 

Download Presentation

C14,A15,D11,C08,C11,A02

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1010E Programming MethodologyTutorial 1Basic 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 … • Email: fan.qi@comp.nus.edu.sg • 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. 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 

  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 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?

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

  10. Question: how do you get ASCII table search online?

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

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

  13. 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)

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

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

  16. 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?

  17. Thank you ! • See you next week! 

More Related