CSCI 159 Quiz 1 Sample Solutions (F24N01/02) Wednesday 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 firstNum 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.

Sample solution
float firstNum;
cout << "Please enter a number: ";
cin >> firstNum;
cout << "You entered " << firstNum << endl;


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

Sample solution
rem = numer % denom;


Question 3 [1.5 marks]
Suppose a, b, and c have already been declared as variables of type double 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 10, with 4 digits of precision after the decimal point for each.

Sample solution
cout << setiosflags(ios::fixed); // the following also works: cout << fixed;
cout << setprecision(4);
cout << setw(10) << a << endl;
cout << setw(10) << b << endl;
cout << setw(10) << c << endl;


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

Sample solution
v = sqrt(pow(w, x) + pow(y, z));


Question 5 [4 marks]
Write a short but complete C++ program that gets the user to enter an integer value, 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 2 then the program would display 2, 4, and 8.)

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

int main()
{
   int userVal; // could use long instead
   cout << "Please enter an integer: ";
   cin >> userVal;
   cout << "You entered " << userVal << endl;
   // the sample solution shows a couple of different computation techniques, either is fine
   cout << "The square of that is " << (userVal*userVal) << endl;
   cout << "The cube of that is " << pow(userVal, 3) << endl;
}