# tutorial04_ans - CS1010E Programming Methodology Semester 1...

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

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS1010E Programming Methodology Semester 1 2011/2012 Week of 12 – 16 September 2011 Tutorial 4 Suggested Answers Control Flow Statements 1. In Lecture #4, several alternative algorithms were introduced to find the maximum of three numbers. (a) The following is yet another version. if (num1 > num2) if (num1 > num3) max = num1; else max = num3; else if (num2 > num3) max = num2; else max = num3; As you can see, depending on the exact values of num1 , num2 and num3 , there are four possible logical paths through the above nested if..else construct. Very often we are required to test selection control constructs by performing complete path testing, i.e. devising sets of values of variables ( num1 , num2 and num3 ) such that all possible paths are tested to be correct ( max ). Give examples of test cases that can be used for complete path testing. Is the algorithm above general? Does it satisfy all possible integer inputs? if (num1 > num2) if (num1 > num3) max = num1; // 3 2 1; 3 1 2; 3 2 2 else max = num3; // 2 1 3; 3 2 3 else if (num2 > num3) max = num2; // 1 3 2; 2 3 1; 2 3 2; 3 3 2 else max = num3; // 1 2 3; 2 2 3; 3 2 3; 3 3 3 1 (b) Suppose you are given the function to find the maximum of two numbers. int max2(int a, int b) { if (a > b) return a; else return b; } Write a program fragment that uses the max2 function defined above to find the maximum of three numbers num1 , num2 and num3 and assign the maximum to the variable max . #include <stdio.h> int max2(int a, int b); int main(void) { int num1, num2, num3, max; printf("Enter three numbers: "); scanf("%d %d %d", &num1, &num2, &num3); max = max2(num1,max2(num2,num3)); printf("The maximum is %d.\n", max); return 0; } /* Function max2 returns the maximum of two arguments a and b. */ int max2(int a, int b) { if (a > b) return a; else return b; } 2 2. The severity of an earthquake is classified using the Richter Magnitude Scale according to the following table. Richter Scale Classification magnitude < 4 . minor 4 . ≤ magnitude < 6 . 0) moderate 6 . ≤ magnitude < 6 . 9) strong magnitude ≥ 6 . 9 major (a) Define four integer constants MINOR , MODERATE , STRONG and MAJOR with appropri- ate integer values. In the remaining questions below, you should only use these constants. (b) Write a function classifyEarthquake that takes in the magnitude of the earth- quake as a double value and returns the classification of the earthquake. What should be the return type of this function?...
View Full Document

## This note was uploaded on 01/05/2012 for the course MATHEMATIC MA1505 taught by Professor Freudleong during the Spring '10 term at National University of Singapore.

### Page1 / 11

tutorial04_ans - CS1010E Programming Methodology Semester 1...

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

View Full Document
Ask a homework question - tutors are online