Chapter 2
C++ How to Program · 14 exercises
Problem 4
Write a statement (or comment) to accomplish each of the following (assume that using directives have been used for cin, cout and end1): a) State that a program calculates the product of three integers. b) Declare the variables \(x, y, z\) and result to be of type int (in separate statements). c) Prompt the user to enter three integers. d) Read three integers from the keyboard and store them in the variables \(x, y\) and \(z\) e) Compute the product of the three integers contained in variables \(x, y\) and \(z,\) and assign the result to the variable result. f) Print "The product is " followed by the value of the variable result. g) Return a value from main indicating that the program terminated successfully.
7 step solution
Problem 7
Discuss the meaning of each of the following objects: a) std: :cin b) std: :cout
2 step solution
Problem 11
Fill in the blanks in each of the following: a) What arithmetic operations are on the same level of precedence as multiplication? b) When parentheses are nested, which set of parentheses is evaluated first in an arithmetic expression? c) A location in the computer's memory that may contain different values at various times throughout the execution of a program is called a(n)
3 step solution
Problem 14
Given the algebraic equation y = ax 3 + 7, which of the following, if any, are correct C++ statements for this equation? a) y = a*x* x*x+ 7; b) y = a*x* x*(x + 7 ); c) y = (a*x )*x* (x+ 7 ); d) y = (a * x) * x*x + 7;
6 step solution
Problem 15
(Order of Evalution) State the order of evaluation of the operators in each of the following C++ statements and show the value of x after each statement is performed. a) x = 7 + 3 * 6 / 2 - 1; b) x = 2 % 2 + 2 * 2 - 2 / 2; c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
3 step solution
Problem 16
Write a program that asks the user to enter two numbers, obtains the two numbers from the user and prints the sum, product, difference, and quotient of the two numbers.
4 step solution
Problem 17
Write a program that prints the numbers 1 to 4 on the same line with each pair of adjacent numbers separated by one space. Do this several ways: a) Using one statement with one stream insertion operator. b) Using one statement with four stream insertion operators. c) Using four statements.
3 step solution
Problem 18
Write a program that asks the user to enter two integers, obtains the numbers from the user, then prints the larger number followed by the words "is Targer." If the numbers are equal, print the message "These numbers are equal."
4 step solution
Problem 19
Write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers. The screen dialog should appear as follows:
7 step solution
Problem 20
Write a program that reads in the radius of a circle as an integer and prints the circle's diameter, circumference and area. Use the constant value 3.14159 for \(\pi .\) Do all calculations in output statements. [Note: In this chapter, we've discussed only integer constants and variables. In Chapter 4 we discuss floating-point numbers, i.e., values that can have decimal points.]
4 step solution
Problem 23
Write a program that reads in five integers and determines and prints the largest and the smallest integers in the group. Use only the programming techniques you learned in this chapter.
4 step solution
Problem 25
Write a program that reads in two integers and determines and prints if the first is a multiple of the second. [Hint: Use the modulus operator.]
4 step solution
Problem 28
Write a program that inputs a five-digit integer, separates the integer into its digits and prints them separated by three spaces each. [Hint: Use the integer division and modulus operators.] For example, if the user types in \(42339,\) the program should print:
3 step solution
Problem 31
Research several car-pooling websites. Create an application that calculates your daily driving cost, so that you can estimate how much money could be saved by car pooling, which also has other advantages such as reducing carbon emissions and reducing traffic congestion. The application should input the following information and display the user's cost per day of driving to work: a) Total miles driven per day. b) Cost per gallon of gasoline. c) Average miles per gallon. d) Parking fees per day. e) Tolls per day.
3 step solution