CSCI 159 Quiz 1 (F25N02) Monday lab

The quiz is closed book, closed notes, no electronics permitted, and to be completed as a strictly individual exercise. You are permitted one double-sided 8.5" x 11" sheet of notes.

The quiz is worth a total of 20 marks. Attempt all questions, answering directly on the exam paper.

Note that when a code segment is requested you are only expected to put the requested lines of code, not the rest of a larger program it would be part of. When a complete program is requested, on the other hand, you should provide the entire program (#includes, int main, etc).


Question 1 [3 marks]
Write a C++ code segment that declares a variable named initialVal of type float, uses cout to ask the user to enter a value, uses cin to read their input into the variable, and uses cout to display the entered value back to them.









Question 2 [3 marks]
Suppose num, denom, and rem have already been declared as variables of type long, and that values have already been assigned to num and denom. Write a C++ code segment that uses the modulo operator (%) to compute the remainder after dividing num by denom, storing that value in rem.









Question 3 [3 marks]
Suppose price1, price2, and price3 have already been declared as variables of type float and that values have already been assigned to them. Write a C++ code segment that uses cout, setw, fixed, and setprecision to display them on three separate lines (one value per line) in a column of width 10, with 2 digits of precision after the decimal point for each.








Question 4 [3 marks]
Suppose four variables, w, x, y, z, have already been declared as type double, and that x, y, z, already have values assigned to them. Write a short code segment that computes (xy + the square root of z) and stores the result in w. (You can assume the cmath library has already been #included, so pow and sqrt are available.)








Question 5 [8 marks]
Write a short but complete C++ program that gets the user to enter a real number, stores that value appropriately, displays the value back to the user, then computes and displays twice the original value and then three times that value. (E.g. if the original value was 1.2 then the program would display 2.4 and 7.2.)

Each value displayed should be on a line of its own.