Chapter 03 Checkpoints Flashcards
3.1 What header le must be included in programs using cin?
3.1
iostream
3.2 TRUE or FALSE: cin requires the user to press the [Enter] key when finished
entering data.
3.2 True
3.3 Assume value is an integer variable. If the user enters 3.14 in response to the following
programming statement, what will be stored in value?
cin»_space; value;
A) 3.14
B) 3
C) 0
D) Nothing. An error message is displayed.
3.3 B
3.4 A program has the following variable de nitions.
long miles;
int feet;
float inches;
Write one cin statement that reads a value into each of these variables.
3.4
cin»_space; miles»_space; feet»_space; inches;
3.5 The following program will run, but the user will have dif culty understanding
what to do. How would you improve the program?
// This program multiplies two numbers and displays the result.
#include
using namespace std;
int main()
{
double first, second, product;
cin»_space; first»_space; second;
product = first * second;
cout
3.5 Include one or more
cout
statements explaining what values the user should enter.
3.6 Complete the following program skeleton so it asks for the user s weight (in
pounds) and displays the equivalent weight in kilograms.
#include
using namespace std;
int main()
{
double pounds, kilograms;
// Write code here that prompts the user
// to enter his or her weight and reads
// the input into the pounds variable.
// The following line does the conversion.
kilograms = pounds / 2.2;
// Write code here that displays the user’s weight
// in kilograms.
return 0;
}
3.6 #include using namespace std; int main() { double pounds, kilograms; cout > pounds; // The following line does the conversion. // One kilogram weighs 2.2 pounds. kilograms = pounds / 2.2; cout
3.7 Complete the table below by writing the value of each expression in the Value
column.
Expression Value 6 + 3 * 5 12 / 2 - 4 9 + 14 * 2 - 6 5 + 19 % 3 - 1 (6 + 2) * 3 14 / (11 - 4) 9 + 12 * (8 - 3) (6 + 17) % 2 - 1 (9 - 3) * (6 + 9) / 3
3.7 Value 21 2 31 5 24 2 69 0 30
3.8 Write C++ expressions for the following algebraic expressions:
y = 6x a = 2b + 4c y x2 = g x + 2 z2 = ------------ y x2 z2 = -----
3.8 y = 6 * x; a = 2 * b + 4 * c; y = x * x; or y = pow(x, 2.0); g = (x + 2) / (z * z); or g = (x + 2.0) / pow(z, 2.0); y = (x * x) / (z * z); or y = pow(x, 2.0) / pow (z, 2.0);
3.9 Study the following program and complete the table. #include #include using namespace std; int main() { double value1, value2, value3; cout > value1; value2 = 2 * pow(value1, 2.0); value3 = 3 + value2 / 2 - 1; cout
3.9 If the user enters… The program displays… 2 6 5 27 4.3 20.49 6 38
Complete the following program skeleton so it displays the volume of a cylindrical fuel tank. The formula for the volume of a cylinder is Volume = r2h where is 3.14159 r is the radius of the tank h is the height of the tank #include #include using namespace std; int main() { double volume, radius, height; cout > height; cout > radius; // You must complete the program. }
3.10
#include
#include
using namespace std;
int main() { double volume, radius, height; cout > height; cout > radius; volume = 3.14159 * pow(radius, 2.0) * height; cout
3.11 Assume the following variable de nitions: int a = 5, b = 12; double x = 3.4, z = 9.1; What are the values of the following expressions? A) b / a B) x * a C) static_cast(b / a) D) static_cast(b) / a E) b / static_cast(a) F) static_cast(b) / static_cast(a) G) b / static_cast(x) H) static_cast(x) * static_cast(z) I) static_cast(x * z) J) static_cast(static_cast(x) * static_cast(z))
3.11 A) 2 B) 17.0 C) 2.0 D) 2.4 E) 2.4 F) 2.4 G) 4 H) 27 I) 30 J) 27.0
3.12 Complete the following program skeleton so it asks the user to enter a character. Store the character in the variable letter. Use a type cast expression with the variable in a cout statement to display the character s ASCII code on the screen. #include using namespace std; int main() { char letter; // Finish this program // as specified above. return 0; }
3.12 #include using namespace std; int main() { char letter; cout > letter; cout
3.13 What will the following program display? #include using namespace std; int main() { int integer1, integer2; double result; integer1 = 19; integer2 = 2; result = integer1 / integer2; cout
3.13 9
9.5
9
3.14 Write a multiple assignment statement that assigns 0 to the variables total,
subtotal, tax, and shipping.
3.14
total = subtotal = tax = shipping = 0;
3.15 Write statements using combined assignment operators to perform the following:
A) Add 6 to x.
B) Subtract 4 from amount.
C) Multiply y by 4.
D) Divide total by 27.
E) Store in x the remainder of x divided by 7.
F) Add y * 5 to x.
G) Subtract discount times 4 from total.
H) Multiply increase by salesRep times 5.
I) Divide profit by shares minus 1000.
3.15 A) x += 6; B) amount -= 4; C) y *= 4; D) total /= 27; E) x %= 7; F) x += (y * 5); G) total -= (discount * 4); H) increase *= (salesRep * 5); I) profit /= (shares – 1000);