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”.

is used to search in a predesignated compiler and system directories. Page 6 of 7 . printf(“%d”. That’s why programmer-defined headers are normally placed in the same folder as the program file. The second type (“ “).n++). As can be seen in the sample code. coefficient a can’t be zero.c program with the use of the preprocessor directive. The first type (<>)..500-0. equal real roots or imaginary roots.h is a programmer-configured header file which was included in the myCfile. Sample dialog is as follows Execution 1 Program to…ROOTS of Quadratic Equation. PART VI: Nested If-Else statement Problem statement 2 Write a C-program to determine the roots of a quadratic equation of the form y=ax +bx+c. Input the coefficients in a line: 0 2 2 Sorry. #include.n++). One being enclosed by angle bars and the other being enclosed by quotation marks.) The following code should print the values 1 to 10 n=1. n=1. while (n<10) will only print 1 to 9. while (n<=10) printf(“%d”.Answers to Problems in Worksheet #2. The difference is the location where the preprocessor begins its search for the header file. there are two forms of including a header file. Program has to output all possible cases: distinct. Input the coefficients in a line: 1 1 1 The roots are equal to -0. begins its search for the header file in the same directory as the file being compiled.866i and -0.866i. We normally find the standard library headers in these locations. Execution 2 Program to…ROOTS of Quadratic Equation.. 4. Code PART VII: Pre-processor directives and Header files The myHeader.500+0.

for and do-while loop While Loop Do-while loop For loop Page 7 of 7 .Answers to Problems in Worksheet #2. PART VIII: while.