C Programming Exam Review (Chapter 3)
Write single C statements that: Increment variable i by 1.
++i;
What does the following program print? 1 #include <stdio.h> 2 3 int main( void ) 4 { 5 unsigned int x = 1, total = 0, y; 6 7 while ( x <= 10 ) { 8 y = x * x; 9 printf( "%d\n", y ); 10 total += y; 11 ++x; 12 } // end while 13 14 printf( "Total is %d\n", total ); 15 } // end main
1 4 9 16 25 36 49 64 81 100 Total is 385
Combine the statements that you wrote in Exercise 3.4 into a program that calculates the sum of the integers from 1 to 10. Use the while statement to loop through the calculation and increment statements. The loop should terminate when the value of x becomes 11.
1 // Calculate the sum of the integers from 1 to 10 2 #include 3 4 int main( void ) 5 { 6 unsigned int sum, x; // define variables sum and x 7 ] 8 x = 1; // set x 9 sum = 0; // set sum 10 11 while ( x <= 10 ) { // loop while x is less than or equal to 10 12 sum += x; // add x to sum 13 ++x; // increment x 14 } // end while 15 16 printf( "The sum is: %u\n", sum ); // display sum 17 } // end main function
Write a C program that uses the statements in Exercise 3.7 to calculate x raised to the y power. The program should have a while repetition control statement.
1 // raise x to the y power 2 #include 3 4 int main( void ) 5 { 6 unsigned int x, y, i, power; // define variables 7 8 i = 1; // set i 9 power = 1; // set power 10 printf( "%s", "Enter first integer: " ); 11 scanf( "%u", &x ); // read value for x from user 12 printf( "%s", "Enter second integer: " ); 13 scanf( "%u", &y ); // read value for y from user 14 15 while ( i <= y ) { // loop while i is less than or equal to y 16 power *= x; // multiply power by x 17 ++i; // increment i 18 } // end while 19 20 printf( "%u\n", power ); // display power 21 } // end main function
Identify and correct the errors in each of the following: while ( c <= 5 ) { product *= c; ++c;
Error: Missing the closing right brace of the while body. Correction: Add closing right brace after the statement ++c;.
Identify and correct the errors in each of the following: scanf( "%.4f", &value );
Error: Precision used in a scanf conversion specification. Correction: Remove .4 from the conversion specification.
Identify and correct the errors in each of the following: if ( gender == 1 ) puts( "Woman" ); else; puts( "Man" );
Error: Semicolon after the else part of the if...else statement results in a logic error. The second printf will always be executed. Correction: Remove the semicolon after else.
State which of the following are true and which are false. If a statement is false, explain why. Experience has shown that the most difficult part of solving a problem on a computer is producing a working C program.
False. Developing the algorithm is the most difficult part of solving a problem.
State which of the following are true and which are false. If a statement is false, explain why. Flowlines indicate the actions to be performed.
False. Flowlines indicate the order in which the actions are performed.
State which of the following are true and which are false. If a statement is false, explain why. Conditions written inside decision symbols always contain arithmetic operators (i.e., +, -, *, /, and %).
False. They normally contain conditional operators.
What's wrong with the following while repetition statement (assume z has value 100), which is supposed to calculate the sum of the integers from 100 down to 1: while ( z >= 0 ) sum += z;
The value of the variable z is never changed in the while statement. Therefore, an infinite loop is created. To prevent the infinite loop, z must be decremented so that its value eventually becomes less than zero.
State which of the following are true and which are false. If a statement is false, explain why. A sentinel value must be a value that cannot be confused with a legitimate data value.
True
State which of the following are true and which are false. If a statement is false, explain why. In top-down, stepwise refinement, each refinement is a complete representation of the algorithm.
True
A procedure for solving a problem in terms of the actions to be executed and the order in which the actions should be executed is called a(n) ________.
algorithm
A synonym for procedure is _______.
algorithm
In a flowchart, the order in which the steps should be performed is indicated by ________ symbols.
arrow (flowline)
Rectangle symbols correspond to calculations that are normally performed by ________ statements and input/output operations that are normally performed by calls to the _________ and _________ standard library functions.
assignment, printf, scanf
Several statements grouped together in braces ({ and }) are called a(n) _________.
compound statement
The item written inside a decision symbol is called a __________.
condition
Repetition of a set of instructions a specific number of times is called __________ repetition.
counter-controlled
A ________ is a graphical representation of an algorithm
flowchart
Write single C statements that: Set unsigned integer variable i to 1.
i = 1;
Identify and correct the errors in each of the following. [Note: There may be more than one error in each piece of code.] if ( age >= 65 ); puts( "Age is greater than or equal to 65" ); else puts( "Age is less than 65" );
if ( age >= 65 ) // ; removed puts( "Age is greater than or equal to 65" ); else puts( "Age is less than 65" );
Write a single C statement to accomplish each of the following: Test if the value of the variable count is greater than 10. If it is, print "Count is greater than 10."
if ( count > 10 ) puts( "Count is greater than 10." );
The _________ selection statement is used to execute one action when a condition is true and another action when that condition is false.
if...... else
Write a C statement to accomplish each of the following tasks. Define variables sum and x to be of type int.
int sum, x;
Identify and correct the errors in each of the following. [Note: There may be more than one error in each piece of code.] int x = 1, total; while ( x <= 10 ) { total += x; ++x; }
int x = 1, total = 0; // total not originally initialized while ( x <= 10 ) { total += x; ++x; }
The solution to any problem involves performing a series of actions in a specific _______.
order
Write single C statements that: Multiply unsigned integer variable power by x and assign the result to power.
power *= x;
Write single C statements that: Set unsigned integer variable power to 1.
power = 1;
Write a single C statement to accomplish each of the following: Print the value 123.4567 with 2 digits of precision. What value is printed?
printf( "%.2f", 123.4567 ); 123.46 is displayed.
Write a single C statement to accomplish each of the following: Print the floating-point value 3.14159 with three digits to the right of the decimal point. What value is printed?
printf( "%.3f\n", 3.14159 ); 3.142 is displayed.
Write single C statements that: Output unsigned integer variable power with printf. Use the conversion specifier %u.
printf( "%u", power );
Write a C statement to accomplish each of the following tasks. Print "The sum is: " followed by the value of variable sum.
printf( "The sum is: %d\n", sum );
Write a single C statement to accomplish each of the following: Multiply the variable product by 2 using the *= operator.
product *= 2;
Write a single C statement to accomplish each of the following: Multiply the variable product by 2 using the = and * operators.
product = product * 2;
Specifying the execution order of statements by the computer is called __________.
program control
Write a single C statement to accomplish each of the following: Calculate the remainder after q is divided by divisor and assign the result to q. Write this statement two different ways.
q %= divisor; q = q % divisor;
Write single C statements that: Input unsigned integer variable x with scanf. Use the conversion specifier %u
scanf( "%u", &x );
Write single C statements that: Input unsigned integer variable y with scanf. Use the conversion specifier %u.
scanf( "%u", &y );
When it is not known in advance how many times a set of statements will be repeated, a(n) _________ value can be used to terminate the repetition.
sentinel
A special value used to indicate "end of data entry" is called a _______, a _________, a _________, or a _________ value.
sentinel value, dummy value, signal value, flag value
All programs can be written in terms of three types of control statements: _________, __________, and ___________.
sequence, selection, and repetition
Write a C statement to accomplish each of the following tasks. Add variable x to variable sum and assign the result to variable sum.
sum += x; or sum = sum + x;
Write a C statement to accomplish each of the following tasks. Set variable sum to 0.
sum = 0;
A variable that accumulates the sum of several numbers is a ________.
total
The ________ repetition statement specifies that a statement or group of statements is to be executed repeatedly while some condition remains true.
while
Write single C statements that: Test i to see if it's less than or equal to y in the condition of a while statement.
while ( i <= y )
Identify and correct the errors in each of the following. [Note: There may be more than one error in each piece of code.] While ( x <= 100 ) total += x; ++x;
while ( x <= 100 ) { // while changed to lowercase total += x; ++x; } // } added
Identify and correct the errors in each of the following. [Note: There may be more than one error in each piece of code.] while ( y > 0 ) { printf( "%d\n", y ); ++y; }
while ( y > 0 ) { printf( "%d\n", y ); --y; // ++ changed to --, loop never ends otherwise }
Write a C statement to accomplish each of the following tasks. Set variable x to 1.
x = 1;
Write four different C statements that each add 1 to integer variable x.
x = x + 1; x += 1; ++x; x++;