Chapter 3 MPL – Flashcards
Unlock all answers in this set
Unlock answersquestion
cin >> datum;
answer
Given an int variable datum that has already been declared , write a statement that reads an integer value from standard input into this variable .
question
cin >> val;
answer
Write a statement that reads an integer value from standard input into val. Assume that val has already been declared as an int variable .
question
cin >> temperature;
answer
Write a statement that reads a floating point (real) value from standard input into temperature. Assume that temperature. has already been declared as an double variable .
question
cin >> x;
answer
Write an expression that attempts to read an integer from standard input and store it in an int variable , x, that has already been declared .
question
cin >> x;
answer
Write an expression that attempts to read a double value from standard input and store it in an double variable , x, that has already been declared .
question
(12 + 40) / 2
answer
Write an expression that computes the average of the values 12 and 40.
question
(exam1 + exam2) / 2
answer
Write an expression that computes the average of the variables exam1 and exam2 (both declared and assigned values ).
question
#include using namespace std; int main () { int variable; cin >> variable; cout << (variable * variable); return 0; }
answer
Write a complete program that declares an integer variable , reads a value from the keyboard into that variable , and writes to standard output the square of the variable 's value . Besides the number, nothing else should be written to standard output .
question
#include using namespace std; int main () { int x; cin >> x; cout << x << " " << 2 * x << " " << x * x << endl; return 0; }
answer
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 .
question
(length1 * width1) + (length2 * width2)
answer
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.
question
small + large
answer
A wall has been built with two pieces of sheetrock, a smaller one and a larger one. The length of the smaller one is stored in the variable small. Similarly, the length of the larger one is stored in the variable large. Write a single expression whose value is the length of this wall.
question
(double)(tickets1 + tickets2 + tickets3) / (class1 + class2 + class3)
answer
Three classes of school children are selling tickets to the school play. The number of tickets sold by these classes, and the number of children in each of the classes have been read into these variables :tickets1, tickets2, tickets3 and class1, class2, class3. Write an expression for the average number of tickets sold per school child.
question
(1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + 1.0/5.0 + 1.0/6.0 + 1.0/7.0 + 1.0/8.0)
answer
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.
question
hn - 1.0/(n)
answer
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. Assume that n is an integer variable whose value is some integer N greater than 1. Assume also that hn is a double variable whose value is the Nth harmonic number. Write an expression whose value is the (N-1)th harmonic number.
question
hn + 1.0/(n+1)
answer
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. Assume that n is an integer variable whose value is some positive integer N. Assume also that hn is a double variable whose value is the Nth harmonic number. Write an expression whose value is the (N+1)th harmonic number.
question
cout << price/100 << " dollars and " << price%100 << " cents" << "n";
answer
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".
question
double (distance) / double (speed)
answer
Given two integer variables distance and speed, write an expression that divides distance by speed using floating point arithmetic, i.e. a fractional result should be produced.
question
float (children) / float (families)
answer
Assume that children is an integer variable containing the number of children in a community and that families is an integer variable containing the number of families in the same community. Write an expression whose value is the average number of children per family.
question
bridgePlayers = (bridgePlayers + 4);
answer
Given an integer variable bridgePlayers, write an statement that increases the value of that variable by 4.
question
profits = profits * 10;
answer
Given an integer variable profits, write a statement that increases the value of that variable by a factor of 10.
question
val += 5;
answer
Write a statement using a compound assignment operator to add 5 to val (an integer variable that has already been declared and initialized ).
question
minutes_left -= 10;
answer
Write a statement using a compound assignment operator to subtract 10 from minutes_left (an integer variable that has already been declared and initialized ).
question
pay /= 2;
answer
Write a statement using a compound assignment operator to cut the value of pay in half (pay is an integer variable that has already been declared and initialized ).
question
num_rabbits *= 4;
answer
Write a statement using a compound assignment operator to multiply num_rabbits by 4, changing the value of num_rabbits (num_rabbits has already been declared and initialized ).
question
val %= 16;
answer
Write a statement using a compound assignment operator to change val to the remainder when val is divided by 16 (val has already been declared and initialized )
question
num_items += 1;
answer
Write a statement using the increment operator to increase the value of num_items (an already declared integer variable ) by 1
question
count -= 1;
answer
Write a statement using the decrement operator to decrease the value of count (an already declared integer variable ) by 1.
question
#include
answer
Write the necessary preprocessor directive to enable the use of the stream manipulators like setw and setprecision.
question
cout << setw(10) << m << endl;
answer
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.
question
cout << showpoint << x << "n";
answer
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).
question
cout << setprecision(3) << fixed << x << endl;
answer
Assume that x is a double variable that has been given a value . Write a statement that prints it out with exactly three digits to the right of the decimal point no matter what how big or miniscule its value is.
question
cin >> x1 >> x2 >> x3 >> x4 >> x5; cout << setw (5) << right << x1 << endl; cout << setw (5) << right << x2 << endl; cout << setw (5) << right << x3 << endl; cout << setw (5) << right << x4 << endl; cout << setw (5) << right << x5 << endl;
answer
Write a statement that reads 5 successive integers into these variables that have already been declared : x1 x2x3x4 x5. Then write a statement that prints each out on its own line so that they form a right-justified column with a 5-digit width. If any of the integers are 5-digits in size, then they will start at the very beginning of their lines. For example: |54213 | 8713 | 23 | 147 | 15
question
c = cin.get ();
answer
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 .
question
string line; getline (cin, line);
answer
Declare a string named line and write a statement that reads in the next line of standard input into this variable .
question
"(" + word + ")"
answer
Given a string variable word, write a string expression that parenthesizes the value of word. So, if word contains "sadly", the value of the expression would be the string "(sadly)"
question
"http://" + address
answer
Given a string variable address, write a string expression consisting of the string "http://" concatenated with the variable 's string value . So, if the value of the variable were "www.turingscraft.com", the value of the expression would be "http://www.turingscraft.com".
question
prefix + suffix
answer
Write an expression that concatenates the string variable suffix onto the end of the string variable prefix .
question
#include
answer
Write the necessary preprocessor directive to enable the use of functions like sqrt and sin.
question
sqrt (area)
answer
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.
question
sqrt(pow (a, 2) + pow (b, 2))
answer
If a right triangle has sides of length A, B and C and if C is the largest, then it is called the "hypotenuse" and its length is the square root of the sum of the squares of the lengths of the shorter sides (A and B). Assume that variables a and b have been declared as doubles and that a and b contain the lengths of the shorter sides of a right triangle: write an expression for the length of the hypotenuse.
question
sqrt( area + area)
answer
The area of a square is stored in a double variable named area. Write an expression whose value is length of the diagonal of the square.
question
sqrt(pow(length, 2) + pow (width, 2))
answer
The length of a rectangle is stored in a double variable named length, the width in one named width. Write an expression whose value is the length of the diagonal of the rectangle.
question
#include
answer
Write the necessary preprocessor directive to enable the use of the rand function.
question
#include
answer
Write the necessary preprocessor directive to enable the use of the time function.