## Chapter 3 MPL

cin >> datum; | Given an int variable datum that has already been declared , write a statement that reads an integer value from standard input into this variable . |

cin >> val; | Write a statement that reads an integer value from standard input into val. Assume that val has already been declared as an int variable . |

cin >> temperature; | 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 . |

cin >> x; | 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 . |

cin >> x; | 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 . |

(12 + 40) / 2 | Write an expression that computes the average of the values 12 and 40. |

(exam1 + exam2) / 2 | Write an expression that computes the average of the variables exam1 and exam2 (both declared and assigned values ). |

#include using namespace std; int main () int variable; | Write a complete program that declares an integer variable , |

#include using namespace std; int main () cin >> x; cout << x << " " << 2 * x << " " << x * x << endl; return 0; } | Write a complete program that declares an integer variable , |

(length1 * width1) + (length2 * width2) | 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. |

small + large | 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. |

(double)(tickets1 + tickets2 + tickets3) / (class1 + class2 + class3) | 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. |

(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) | 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. |

hn – 1.0/(n) | 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. |

hn + 1.0/(n+1) | 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. |

cout << price/100 << " dollars and " << price%100 << " cents" << "n"; | 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”. |

double (distance) / double (speed) | 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. |

float (children) / float (families) | 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. |

bridgePlayers = (bridgePlayers + 4); | Given an integer variable bridgePlayers, write an statement that increases the value of that variable by 4. |

profits = profits * 10; | Given an integer variable profits, write a statement that increases the value of that variable by a factor of 10. |

val += 5; | Write a statement using a compound assignment operator to add 5 to val (an integer variable that has already been declared and initialized ). |

minutes_left -= 10; | Write a statement using a compound assignment operator to subtract 10 from minutes_left (an integer variable that has already been declared and initialized ). |

pay /= 2; | 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 ). |

num_rabbits *= 4; | 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 ). |

val %= 16; | 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 ) |

num_items += 1; | Write a statement using the increment operator to increase the value of num_items (an already declared integer variable ) by 1 |

count -= 1; | Write a statement using the decrement operator to decrease the value of count (an already declared integer variable ) by 1. |

#include | Write the necessary preprocessor directive to enable the use of the stream manipulators like setw and setprecision. |

cout << setw(10) << m << endl; | 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. |

cout << showpoint << x << "n"; | 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). |

cout << setprecision(3) << fixed << x << endl; | 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. |

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; | 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 |

c = cin.get (); | 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 . |

string line; getline (cin, line); | Declare a string named line and write a statement that reads in the next line of standard input into this variable . |

“(” + word + “)” | 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)” |

“http://” + address | 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”. |

prefix + suffix | Write an expression that concatenates the string variable suffix onto the end of the string variable prefix . |

#include | Write the necessary preprocessor directive to enable the use of functions like sqrt and sin. |

sqrt (area) | 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. |

sqrt(pow (a, 2) + pow (b, 2)) | 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. |

sqrt( area + area) | 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. |

sqrt(pow(length, 2) + pow (width, 2)) | 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. |

#include | Write the necessary preprocessor directive to enable the use of the rand function. |

#include | Write the necessary preprocessor directive to enable the use of the time function. |