Chapter 3 Expressions and Interactivity – Flashcards
24 test answers
Unlock all answers in this set
Unlock answers 24question
Write a complete program that declares an integer variable , reads a value from the keyboard into that variable , and writes to standard output the variable 's value , twice the value , and the square of the value , separated by spaces. Besides the numbers, nothing else should be written to standard output except for spaces separating the values . Instructor Notes: Remember 1)include statement for iostream 2) namespace line 3) main function 4) declare and integer k 5)get k from the console (cin) 6) cout statement
answer
#include using namespace std; int main() { int random; cin >> random ; cout << random << " " ; cout << random * 2 << " " ; cout << random * random ; return 0; }
Unlock the answer
question
The dimensions (width and length) of room1 have been read into two variables : width1 and length1. The dimensions of room2 have been read into two other variables : width2 and length2. Write a single expression whose value is the total area of the two rooms.
answer
(width1* length1) + (width2* length2)
Unlock the answer
question
Each of the walls of a room with square dimensions has been built with two pieces of sheetrock, a smaller one and a larger one. The length of all the smaller ones is the same and is stored in the variable small. Similarly, the length of all the larger ones is the same and is stored in the variable large. Write a single expression whose value is the total area of this room. DO NOT use the pow function.
answer
(small+large) * (small + large)
Unlock the answer
question
In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Write an expression whose value is the 8th harmonic number.
answer
(small+large) * (small + large)1.0 / 7.0 + 1.0 / 6.0 + 1.0 / 5.0 + 1.0 / 4.0 + 1.0 / 3.0 + 1.0 / 2.0 + 1 + 1.0/8.0
Unlock the answer
question
Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the number of single dollars that would have to be paid.
answer
price/100
Unlock the answer
question
Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Write a statement that prints the value of price in the form "X dollars and Y cents". So, if the value of price was 4321, your code would print "43 dollars and 21 cents". If the value was 501 it would print "5 dollars and 1 cents". If the value was 99 your code would print "0 dollars and 99 cents".
answer
cout << price / 100 << " dollars and " << price % 100 << " cents";
Unlock the answer
question
Declare an int constant , MonthsInYear, whose value is 12.
answer
const int MonthsInYear=12;
Unlock the answer
question
Declare an int constant MonthsInDecade whose value is the value of the constant MonthsInYear (already declared ) multiplied by 10.
answer
const int MonthsInDecade = MonthsInYear * 10;
Unlock the answer
question
Given an integer variable profits, write a statement that increases the value of that variable by a factor of 10.
answer
profits= profits* 10;
Unlock the answer
question
Write a statement using a compound assignment operator to add 5 to val (an integer variable that has already been declared and initialized ).
answer
val= val + 5;
Unlock the answer
question
Write a statement using a compound assignment operator to subtract 10 from minutes_left (an integer variable that has already been declared and initialized ).
answer
minutes_left -= 10;
Unlock the answer
question
Write a statement using the increment operator to increase the value of num_items (an already declared integer variable ) by 1.
answer
num_items++;
Unlock the answer
question
Assume that m is an int variable that has been given a value . Write a statement that prints it out in a print field of 10 positions.
answer
cout << setw (10) << m;
Unlock the answer
question
Assume that x is a double variable that has been initialized . Write a statement that prints it out, guaranteed to have a decimal point, but without forcing scientific (also known as exponential or e-notation).
answer
cout << showpoint << x;
Unlock the answer
question
A variable c of type char has been declared . Write the necessary code to read in the next character from standard input and store it in c, regardless of whether is a whitespace character .
answer
cin.get(c);
Unlock the answer
question
Declare a string named line and write a statement that reads in the next line of standard input into this variable .
answer
string line; getline (cin,line);
Unlock the answer
question
Assume that c is a char variable has been declared . Write some code that reads in the first character of the next line into c. Assume that the lines of input are under 100 characters long.
answer
cin.ignore(20,'n'); cin.get(c);
Unlock the answer
question
Write the necessary preprocessor directive to enable the use of the stream manipulators like setw and setprecision.
answer
#include
Unlock the answer
question
Write the necessary preprocessor directive to enable the use of functions like sqrt and sin.
answer
#include
Unlock the answer
question
The area of a square is stored in a double variable named area. Write an expression whose value is length of one side of the square.
answer
sqrt(area)
Unlock the answer