Home
>
Articles
>
Programming
>
C/C++
From the author of
Self-Review Exercises
2.1 |
Fill in the blanks in each of the following.
- Every C++ program begins execution at the function _______.
- A(n) ________ begins the body of every function and a(n) ________ ends the body.
- Every C++ statement ends with a(n) ________.
- The escape sequence \n represents the _______ character, which causes the cursor to position to the beginning of the next line on the screen.
- The _______ statement is used to make decisions.
|
2.2 |
State whether each of the following is true or false. If false, explain why. Assume the statement using std::cout; is used.
- Comments cause the computer to print the text after the // on the screen when the program is executed.
- The escape sequence \n, when output with cout and the stream insertion operator, causes the cursor to position to the beginning of the next line on the screen.
- All variables must be declared before they're used.
- All variables must be given a type when they're declared.
- C++ considers the variables number and NuMbEr to be identical.
- Declarations can appear almost anywhere in the body of a C++ function.
- The modulus operator (%) can be used only with integer operands.
- The arithmetic operators *, /, %, + and - all have the same level of precedence.
- A C++ program that prints three lines of output must contain three statements using cout and the stream insertion operator.
|
2.3 |
Write a single C++ statement to accomplish each of the following (assume that using directives have not been used):
- Declare the variables c, thisIsAVariable, q76354 and number to be of type int.
- Prompt the user to enter an integer. End your prompting message with a colon (:) followed by a space and leave the cursor positioned after the space.
- Read an integer from the user at the keyboard and store it in integer variable age.
- If the variable number is not equal to 7, print "The variable number is not equal to 7".
- Print the message "This is a C++ program" on one line.
- Print the message "This is a C++ program" on two lines. End the first line with C++.
- Print the message "This is a C++ program" with each word on a separate line.
- Print the message "This is a C++ program". Separate each word from the next by a tab.
|
2.4 |
Write a statement (or comment) to accomplish each of the following (assume that using directives have been used for cin, cout and endl):
- State that a program calculates the product of three integers.
- Declare the variables x, y, z and result to be of type int (in separate statements).
- Prompt the user to enter three integers.
- Read three integers from the keyboard and store them in the variables x, y and z.
- Compute the product of the three integers contained in variables x, y and z, and assign the result to the variable result.
- Print "The product is " followed by the value of the variable result.
- Return a value from main indicating that the program terminated successfully.
|
2.5 |
Using the statements you wrote in Exercise 2.4, write a complete program that calculates and displays the product of three integers. Add comments to the code where appropriate. [Note: You'll need to write the necessary using directives.]
|
2.6 |
Identify and correct the errors in each of the following statements (assume that the statement using std::cout; is used):
-
if ( c < 7 );
cout << "c is less than 7\n";
-
if ( c => 7 )
cout << "c is equal to or greater than 7\n";
|