lecture05_4on1 - CS1010E Lecture 5 Problem Solving with...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS1010E Lecture 5 Problem Solving with Control Flow Statements Henry Chia hchia@comp.nus.edu.sg Semester 1 2011 / 2012 Department of Computer Science School Of Computing National University Of Singapore CS1010E Lecture 5 – p.1/40 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 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 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 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 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 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 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 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 switch Statement The switch statement is used for multiple-selection decision making....
View Full Document

This note was uploaded on 12/13/2011 for the course ELECTRICAL CS1010E taught by Professor Henrytan during the Spring '11 term at National University of Singapore.

Page1 / 10

lecture05_4on1 - CS1010E Lecture 5 Problem Solving with...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online