CSCI 159 Quiz 1 (F24N01/02) 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 10 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 [1.5 marks]
Write a C++ code segment that declares a variable named userVal of type int,
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 [1.5 marks]
Suppose N, D, and R have already been declared as variables of type int, and that values have
already been assigned to N and D. Write a C++ code segment that uses the modulo operator (%)
to compute the remainder after dividing N by D, storing that value in R.
Question 3 [1.5 marks]
Suppose x, y, and z 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, cin, setw, and
setprecision to display them three lines as a column of width 12, with 3 digits of precision after the
decimal point for each.
Question 4 [1.5 marks]
Suppose five variables, a, b, c, d, f, have already been declared as type double,
and that a, b, c, and d already have values assigned to them. Write a short code segment
that computes the square root of (ab + cd) and stores the result in f.
(You can assume the cmath library has already been #included so pow and sqrt are available.)
Question 5 [4 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, and computes and displays the square of the number
and its cube. (E.g. if the original value was 3 then the program would display 3, 9, and 27.)