Você está na página 1de 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

Answers to Problems in Worksheet #2.

PART II: If and If/Else statement


Dangling Else Problem
1.
2.
3.

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.
Rewrite the code applying the proper indentations that signifies the sub-processes.
Draw the corresponding flow chart.
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

Answers to Problems in Worksheet #2.

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.

Page 3 of 7

Answers to Problems in Worksheet #2.

Commonly encountered problems:


1.

2.

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


Page 4 of 7

Answers to Problems in Worksheet #2.

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 <=10); Syntax Error
x=1;
x++; Enclosure and ORDER
while (x <=10)
printf(%d,x); //aims to print every value of x
printf(%d,x++);

2.) for (y=.1; y != 1.0 ; y+= .1) must be of integer value or change the logical operator
printf(%f\n,y);
for (y=.1; y <= 1.0 ; y+=.1)
for (y=1; y != 10 ; y+=1)
printf(%f\n,y);
printf(%f\n,(float)y/10);

3.) switch (n) {


case 1:
printf( The number is 1\n); no break
case 2:
printf(The number is 2 \n);
break;
default:
printf(The number is not 1 or 2\n);
break;
}

switch (n) {
case 1:
printf( The number is 1\n);
break;
case 2:
printf(The number is 2 \n);
break;
default:
printf(The number is not 1
or 2\n);
break;
}

Page 5 of 7

Answers to Problems in Worksheet #2.

4.) The following code should print the values 1 to 10


n=1;
while (n<10) will only print 1 to 9.
printf(%d,n++);

n=1;
while (n<=10)
printf(%d,n++);

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.
Program has to output all possible cases: distinct, equal real roots or imaginary roots. Sample
dialog is as follows
Execution 1
Program toROOTS of Quadratic Equation..
Input the coefficients in a line: 0 2 2
Sorry, coefficient a cant be zero.
Execution 2
Program toROOTS of Quadratic Equation..
Input the coefficients in a line: 1 1 1
The roots are equal to -0.500+0.866i and -0.500-0.866i.
Code

PART VII: Pre-processor directives and Header files


The myHeader.h is a programmer-configured header file which was included in the myCfile.c program with the use of the preprocessor directive, #include. As can be seen in the sample code, there are two forms of including a header file. One being
enclosed by angle bars and the other being enclosed by quotation marks. The difference is the location where the preprocessor
begins its search for the header file. The first type (<>), is used to search in a predesignated compiler and system directories. We
normally find the standard library headers in these locations. The second type ( ), begins its search for the header file in the
same directory as the file being compiled. Thats why programmer-defined headers are normally placed in the same folder as the
program file.
Page 6 of 7

Answers to Problems in Worksheet #2.

PART VIII: while, for and do-while loop


While Loop

Do-while loop

For loop

Page 7 of 7

Você também pode gostar