lecture05 - CS1010E Lecture 5 Problem Solving with Control...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
CS1010E Lecture 5 Problem Solving with Control Flow Statements Henry Chia [email protected] Semester 1 2011 / 2012 Department of Computer Science School Of Computing National University Of Singapore CS1010E Lecture 5 – p.1/40
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lecture Outline Problem based approach to introduce more selection and repetition constructs. Other selection constructs: switch statement Other repetition constructs: do .. while statement for statement Count-controlled vs Sentinel-controlled input. Shorthand operators. Programming exercises. CS1010E Lecture 5 – p.2/40
Image of page 2
Problem Solving: Sum Digits Write a function sumDigits to find the sum of every digit in a given number n . int sumDigits( int n); How to access each digit when the number of digits is not fixed? Summing from left to right, or right to left? Use a loop? What is the loop termination condition? How to generalize the statement body? CS1010E Lecture 5 – p.3/40
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Problem Solving: Sum Digits / * Function sumDigits returns the sum of the individual digits of n. * / int sumDigits( int n) { int sum = 0; while (n > 0) { sum = sum + n%10; n = n/10; } return sum; } CS1010E Lecture 5 – p.4/40
Image of page 4
Problem Solving: Validate Matric Write a function computeCheckDigit to compute the check digit given the last 5 digits of the matric number A00dddddX. int computeCheckDigit( int n); 1. sum sumDigits ( num ) 2. checkDigit 13 - sum %13 If matric is U0 d 1 d 2 d 3 d 4 d 5 , then sum 2 × d 1 + 6 × d 2 + 2 × d 3 + 4 × d 4 + d 5 CS1010E Lecture 5 – p.5/40
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Problem Solving: Validate Matric Write a main function to perform the following: 1. Read a 5-digit num as input; 2. checkDigit computeCheckDigit ( num ) ; 3. Output the corresponding check letter according to the following table. 1 2 3 4 5 6 7 8 9 10 11 12 13 B A E H J L M N R U W X Y If matric is U 0 d 1 d 2 d 3 d 4 d 5 , use the following table. 1 2 3 4 5 6 7 8 9 10 11 12 13 M B N A R E U H W J X L Y CS1010E Lecture 5 – p.6/40
Image of page 6
Problem Solving: Validate Matric / * Function computeCheckDigit returns the check digit of num. * / int computeCheckDigit( int num) { int sum, check; sum = sumDigits(num); check = 13 - sum%13; return check; } CS1010E Lecture 5 – p.7/40
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Problem Solving: Validate Matric Implementing the output. Multiple simple if s. if (checkDigit == 1) printf( "B\n" ); if (checkDigit == 2) printf( "A\n" ); ... // other cases as if if (checkDigit == 13) printf( "Y\n" ); Nested if .. else s. if (checkDigit == 1) printf( "B\n" ); else if (checkDigit == 2) printf( "A\n" ); ... // other cases as nested if..else else if (checkDigit == 12) printf( "X\n" ); else printf( "Y\n" ); CS1010E Lecture 5 – p.8/40
Image of page 8
switch Statement The switch statement is used for multiple-selection decision making. It is often used to replace nested if .. else statements. switch (int_expression) { case label1: statement(s); case label2: statement(s); // other cases default : statements(s); } CS1010E Lecture 5 – p.9/40
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
switch Statement int_expression : An expression that is evaluated to an integer-typed value.
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern