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\nrepresents 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 istrueorfalse. Iffalse, explain why. Assume the statementusing 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 withcoutand 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 variablesnumberandNuMbErto 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 usingcoutand the stream insertion operator.
|
2.3 |
Write a single C++ statement to accomplish each of the following (assume thatusingdirectives have not been used):
- Declare the variablesc,thisIsAVariable,q76354andnumberto be of typeint.
- 提示用户输入一个整数。结束你的舞会pting 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 variableage.
- If the variablenumberis not equal to7, 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 withC++.
- 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 thatusingdirectives have been used forcin,coutandendl):
- State that a program calculates the product of three integers.
- Declare the variablesx,y,zandresultto be of typeint(in separate statements).
- Prompt the user to enter three integers.
- Read three integers from the keyboard and store them in the variablesx,yandz.
- Compute the product of the three integers contained in variablesx,yandz, and assign the result to the variableresult.
- Print"The product is "followed by the value of the variableresult.
- Return a value frommainindicating 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 necessaryusingdirectives.] |
2.6 |
Identify and correct the errors in each of the following statements (assume that the statementusing 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";
|