Answers to Problems in Worksheet #2.
Page 1 of 7
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.
( ) ( )
Answers to Problems in Worksheet #2.
Page 2 of 7
PART II: If and If/Else statement
Dangling Else Problem
1. 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.
2. Rewrite the code applying the proper indentations that signifies the sub-processes.
3. Draw the corresponding flow chart.
PROBLEM #1:
if ( x < 10 )
if ( y < 10 )
printf(****\n)
else
printf(####\n)
printf($$$$\n)
PROBLEM #2:
if ( x < 10 ){
if ( y < 10 )
printf(****\n)
}
else {
printf(####\n)
printf($$$$\n)
}
Code #1 with indentations:
if ( x < 10 )
if ( y < 10 )
printf(****\n)
else
printf(####\n)
printf($$$$\n)
Code #2 with indentations:
if ( x < 10 ){
if ( y < 10 )
printf(****\n)
}
else {
printf(####\n)
printf($$$$\n)
}
x < 10
y < 10
$$$$
****
####
yes
no
yes
no
x < 10
y < 10
####
$$$$
****
yes no
yes
no
Answers to Problems in Worksheet #2.
Page 3 of 7
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. Sample dialog is as follows:
Program toOPERATIONS.. Input two real numbers
8 0.5
Select an operation (+, *, /): *
The result of the multiplication is 4.000. Thank you for using this program.
Requirements: Make use of a switch statement and take note of the recommended styles
(indentions, scopes,) discussed in class. In addition, program has to trap errors in selected operation. Example, Select an operation (+, *, /): %
Operation undefined for real numbersTry again. Terminating program.
Answers to Problems in Worksheet #2.
Page 4 of 7
Commonly encountered problems:
1. Scanning a subsequent variable of char data type.
Some of you might encounter a problem with retrieving the operation. Upon entering the operation, the program
automatically goes into the default case. To solve this put a SPACE before %c in scanning for the char variable data
type.
2. Syntax Errors:
a. switch (choice) ;
b. For a character data type of variable in a switch operation, we seldom forget to enclose the cases with single
quotation marks.
case + :
c. break ();
PART IV: BREAK and CONTINUE
Exercise #8: Break
Exercise #8: Continue
Answers to Problems in Worksheet #2.
Page 5 of 7
Explanation:
REFERENCE: Exactly taken from the reference book: C How to Program 6th edition by Deitel and Deitel.
Pages 114-115.
PART V: Debugging Selection and Repetition Structures
Find the error in each of the following code segments and explain how to correct it. 1.) x=1; while (x
Answers to Problems in Worksheet #2.
Page 6 of 7
4.) The following code should print the values 1 to 10 n=1; while (n
Answers to Problems in Worksheet #2.
Page 7 of 7
PART VIII: while, for and do-while loop
While Loop
Do-while loop
For loop