Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

University of Technology, Jamaica

School of Computing & Information Technology


Programming II – Tutorial Week 3

1. Fill in the blanks in each of the following.


a) Counter-controlled repetition is also known as _____________________ repetition because it’s

known in advance how many times the loop will be executed.

b) Sentinel-controlled repetition is also known as _______________________ repetition because it’s

not known in advance how many times the loop will be executed.

c) In counter-controlled repetition, a(n) _____________________ is used to count the number of

times a group of instructions should be repeated.

d) The __________________________________ statement, when executed in a repetition

statement, causes the next iteration of the loop to be performed immediately.

e) The ________________________________ statement, when executed in a repetition statement or

a switch, causes an immediate exit from the statement.

f) The _______________________________ is used to test a particular variable or expression for

each of the constant integral values it may assume.

2. State whether the following are true or false. If the answer is false, explain why.
a) The default case is required in the switch selection statement.

b) The break statement is required in the default case of a switch selection statement.

c) The expression (x > y && a < b) is true if either x > y is true or a < b is true.

d) An expression containing the || operator is true if either or both of its operands is true.

3. Write a statement or a set of statements to accomplish each of the following tasks:


a) Sum the odd integers between 1 and 99 using a for statement. Assume the integer variables
sum and count have been defined.

b) Print the value 333.546372 in a field width of 15 characters with precisions of 1, 2, 3, 4 and 5.
Left justify the output. What are the five values that print?

c) Calculate the value of 2.5 raised to the power of 3 using the pow function. Print the result with
a precision of 2 in a field width of 10 positions. What is the value that prints?

d) Print the integers from 1 to 20 using a while loop and the counter variable x. Assume that the
variable x has been defined, but not initialized. Print only five integers per line.
Hint: Use the calculation x % 5. When the value of this is 0, print a newline character,
otherwise print a tab character.]

e) Repeat Exercise 4.3 (d) using a for statement.

4. Find the error in each of the following. (Note: There may be more than one error.)
a) for ( x = 100, x >= 1, x++ )
printf( "%d\n", x );

b) The following code should print whether a given integer is odd or even:
switch ( value % 2 ) {
case 0:
printf( "Even integer\n" );
case 1:
printf( "Odd integer\n" );
}

c) The following code should input an integer and a character and print them. Assume the user types
as input 100 A.
scanf( "%d", &intVal );
charVal = getchar();
printf( "Integer: %d\nCharacter: %c\n", intVal, charVal );

d) for ( x = .000001; x == .0001; x += .000001 ) {


printf( "%.7f\n", x );
}

e) The following code should output the odd integers from 999 to 1:
for ( x = 999; x >= 1; x += 2 ) {
printf( "%d\n", x );
}
f) The following code should output the even integers from 2 to 100:
counter = 2;
Do {
if ( counter % 2 == 0 ) {
printf( "%d\n", counter );
}
counter += 2;
} While ( counter < 100 );

g) The following code should sum the integers from 100 to 150 (assume total is initialized to 0):
for ( x = 100; x <= 150; x++ ); {
total += x;
}

5. Complete the following exercises from the C How To Program text.

4.9, 4.10, 4.11, 4.12, 4.13, 4.14, 4.16, 4.17, and 4.18

You might also like