CSCI 159 Quiz 1 Sample Solutions (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.

Sample solution
int userVal;
cout << "Please enter an integer value" << endl;
cin >> userVal;
cout << "You entered " << userVal << endl;


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.

Sample solution
R = N % D;


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.

Sample solution
cout << setiosflags(ios::fixed); // the following also works: cout << fixed;
cout << setprecision(3);
cout << setw(12) << x << endl;
cout << setw(12) << y << endl;
cout << setw(12) << z << endl;


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

Sample solution
f = sqrt(pow(a, b) + pow(c, d));


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

Sample solution
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int main()
{
   float userVal; // could use double instead
   cout << "Please enter a number: ";
   cin >> userVal;
   cout << "Your value was " << userVal << endl;
   // the sample solution shows a couple of computation approaches, either is fine
   cout << "The square of that is " << (userVal*userVal) << endl;
   cout << "And the cube of your value is " << pow(userVal, 3) << endl;
}