1. (Exercise 1.7) Write a complete program that calculates and prints the product of three integers.
2. (Exercise 2.1) Answer each of the following questions.
a) All programs can be written in terms of three types of control structures: _____, _____, and _____.
b) The _____ selection structure is used to execute one action when a condition is true and another action when that condition is false.
c) Repetition of a set of instructions a specific number of times is called _____ repetition.
d) When it is not known in advance how many times a set of statements will be repeated, a _____ value can be used to terminate repetition.
3. (Exercise 2.2) Write four different C++ statements the each add 1 to integer variable x.
4. (Exercise 2.10) What is wrong with the following while repetition structure:
while ( z >= 0)
sum += z;
5. (Exercise 2.11) 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 structure.
b) The break statement is required in the default case of a switch selection structure.
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.
6. (Exercise 3.2) For the following program, state the scope (either function scope, file scope, block scope, or function prototype scope) of each of the following elements.
a) The variable x in main.
b) The variable y in cube.
c) The function cube.
d) The function main.
e) The function prototype for cube.
f) The identifier y in the function prototype for cube.
----------------------------------------------------------------------------------------------------
#include <iostream.h>
int cube(int y);
main() {
int x;
for (x = 1; x <= 10; x++)
cout << cube(x) << endl;
}
int cube(int y) {
return y * y * y;
}
----------------------------------------------------------------------------------------------------
7. (Exercise 3.5) Give the function prototype for each of the following:
a) Function hypotenuse that takes two double-precision, floating-point arguments, side1 and side2, and returns a double-precision, floating-point result.
b) Function smallest that takes three integers, x, y, z, and returns an integer.
c) Function instructions that does not receive any arguments and does not return a value.
d) Function intToFloat that takes an integer argument, number, and returns a floating-point result.
8. (Exercise 3.7) Find the error in each of the following program segments and explain how the error can be corrected:
a) int g(void) {
cout << "Inside function g" << endl;
int h(void) {
cout << "Inside function h" << endl;
}
b) int sum(int x, int y) {
int result;
result = x + y;
}
9. (Exercise 3.10) Write a complete C++ program that uses an inline function sphereVolume to prompt the user for the radius of a sphere, and to calculate and print the volume of that sphere using the assignment volume = (4/3) * 3.14.159 * pow(radius, 3).
10. (Exercise 4.2) State whether the following are true or false. If the answer is false, explain why.
a) An array can store many different types of values.
b) An array subscript should normally be of data type float.
c) If there are fewer initializers in an initializer list than the number of elements in the array, the remaining elements are automatically initialized to the last value in the list of initializers.
d) It is an error if an initializer list contains more initializers than there are elements in the array.
e) An individual array element that is passed to a function and modified in that function will contain the modified value when the called function completes execution.
11. (Exercise 4.3) Answer the following questions regarding an array called fractions.
a) Define a constant variable arraySize initialized to 10.
b) Declare an array with arraySize elements of type float and initialize the elements to 0.
c) Name the fourth element from the beginning of the array.
d) Refer to array element 4.
e) Assign the value 1.667 to array element 9.
f) Assign the value 3.333 to the seventh element of the array.
g) Print array elements 6 and 9 with two digits of precision to the right of the decimal point and show the output that is actually displayed on the screen.
h) Print all the elements of the array using a for repetition structure. Define the integer variable x as a control variable for the loop. Show the output.
12. (Exercise 5.1) Answer each of the following:
a) A pointer is a variable that contains as its value the _____ of another variable.
b) The three values that can be used to initialize a pointer are _____, _____, or _____.
c) The only integer that can be used to initialize a pointer is _____.