You are on page 1of 7

# Answers to Problems in Worksheet #2.

PART I: Pre and Post Increment and Assignment Operators
Exercise #5: Sample Code

Application
Determine the values of all the variables after the calculation is performed. Assume that when each statement begins executing
all variables have the value of 5.
1.
( )

(

)

2.
(
(

)
)

(
(

)
)

3.
(
(

)
)

Page 1 of 7

2. PROBLEM #2: PROBLEM #1: if ( x < 10 ){ if ( y < 10 ) printf(“****\n”) } else { printf(“####\n”) printf(“\$\$\$\$\n”) } if ( x < 10 ) if ( y < 10 ) printf(“****\n”) else printf(“####\n”) printf(“\$\$\$\$\n”) Code #1 with indentations: Code #2 with indentations: if ( x < 10 ) if ( y < 10 ) printf(“****\n”) else printf(“####\n”) printf(“\$\$\$\$\n”) if ( x < 10 ){ if ( y < 10 ) printf(“****\n”) } else { printf(“####\n”) printf(“\$\$\$\$\n”) } x < 10 no yes yes y < 10 yes “****” no “####” “\$\$\$\$” x < 10 no “####” “\$\$\$\$” no y < 10 yes “****” Page 2 of 7 . Draw the corresponding flow chart. 3. PART II: If and If/Else statement Dangling Else Problem 1.Answers to Problems in Worksheet #2. Rewrite the code applying the proper indentations that signifies the sub-processes. Determine the output for each of the following set of codes when x is 9 an y is 11 and when x is 11 and y is 9.

Page 3 of 7 . In addition. Terminating program. PART III: Switch statement Exercise #6: Familiarizing with Switch… Exercise #7: Switch Application Write a C-program that will accept two real numbers and perform an operation specified by the user.Answers to Problems in Worksheet #2. *. scopes. /): * The result of the multiplication is 4. *. Example. Thank you for using this program. Requirements: Make use of a switch statement and take note of the recommended styles (indentions. Input two real numbers 8 0.5 Select an operation (+.…) discussed in class.. Select an operation (+. program has to trap errors in selected operation. Sample dialog is as follows: Program to…OPERATIONS. /): % Operation undefined for real numbers…Try again.000.

case + : c. Syntax Errors: a. 2. Some of you might encounter a problem with retrieving the operation. we seldom forget to enclose the cases with single quotation marks. Scanning a subsequent variable of “char” data type. To solve this put a “SPACE” before %c in scanning for the char variable data type. b. break (). PART IV: BREAK and CONTINUE… Exercise #8: Break Exercise #8: Continue Page 4 of 7 . the program automatically goes into the default case.Answers to Problems in Worksheet #2. Commonly encountered problems: 1. switch (choice) . For a character data type of variable in a switch operation. Upon entering the operation.

for (y=.x++). y+=1) printf(“%f\n”.Answers to Problems in Worksheet #2.1. 2.) for (y=. Syntax Error x=1. Enclosure and ORDER while (x <=10) printf(“%d”.0 . PART V: Debugging Selection and Repetition Structures Find the error in each of the following code segments and explain how to correct it. Pages 114-115. break. } Page 5 of 7 .y). break. y != 10 . while (x <=10). Explanation: REFERENCE: Exactly taken from the reference book: “C How to Program” 6th edition by Deitel and Deitel. case 2: printf(“The number is 2 \n”).0 . default: printf(“The number is not 1 or 2\n”).) x=1.x).(float)y/10). 3.1) for (y=1. y != 1. break. break. break. default: printf(“The number is not 1 or 2\n”).1. } switch (n) { case 1: printf(“ The number is 1\n”). //aims to print every value of x printf(“%d”. printf(“%f\n”. 1. y+= .) switch (n) { case 1: printf(“ The number is 1\n”). no break case 2: printf(“The number is 2 \n”). y+=. x++. y <= 1.y).1) must be of integer value or change the logical operator printf(“%f\n”.