Sample paper: pdf, short pdf, source, solution: pdf, short pdf, source.
Printable versions: PS1 PS2 PS3 PS4 PS for Reading Week PS5 PS6 PS7 PS8.
PS 1
- What value does a variable store if you declare it without
assigning a value to it: we write int a for example and then print out
a; what about doubles, floats and chars.
- If you take a double from an unsigned int does it cast to a double or a short int?
- Write a short programme that cin's a double and prints out the decimal part.
- ceil(a) rounds a up and floor(a) rounds it down, write a programme that cin's a double and then rounds it the way we do, up if it the decimal is greater than 0.5 and down otherwise.
solution
PS 2
- rand() gives a random, or strictly speaking pseudo-random, int between 0 and RAND_MAX, often the largest integer. Use rand() and % to print out a random number between one and seven.
- The Fortune Teller Fish is a party favor that comes in Christmas crackers, it is a red cellophane fish that twists or curls up when you put it on your hand, something to do with moisture. It purports to tell your fortune based on the shape it makes and answers one of Jealousy, Indifference, In Love, Fickle, False, Dead One and Passionate. Under the assumption all seven are equally likely, write a Fortune Teller Fish emulator, remember, you should use else if's as well as if's to avoid testing possibilities that have already been ruled out.
- char's are stored as short integers, basically between 0 and 127. The details of this encoding are historic but all the numbers from 32 to 127 correspond to printable symbols, the first 32 were reserves for control characters, characters used to control printers and such. Most of these early characters are now obsolete, some are still used for carriage returns, tabs and such like. By casting int to char print out the 96 printable symbols. Use a double loop to print them out as in an array rather than a long list.
- The do/while loop is like a while loop except it tests the condition at the end:
do
{
STATEMENTS;
}
while(CONDITION);
The advantage is that you do the loop at least once. Write a programme that says "Input any word that isn't pointless" and keeps asking until you enter a word that isn't "pointless" and the programme finishes.
solution
PS 3
- A simple primality testing programme works like this.
- Check if the number is 2 or 3.
- Check it is of the form 6n+/-1, all primes that aren't 2 and 3 are of this form.
- Divide it by every odd number not greater than its square root.
Write a primality testing programme along these lines, it should also give two factors if there are factors. Do this in a way that has at least two functions, the first should return a bool telling you if the number is of the form 6n+/-1, the second should return a bool telling you if the number is divisible by another number m and should use pass-by-reference to return the value of the result of that division.
- Write a programme which uses a recursive function to print a word backwards, in other words, if the string passed to it is empty it should return, if it has letters in it, it should print the last one and call itself on the string with the last letter removed. The string methods you need are .length() which gives the length, .at(int i) which returns the ith char and .erase(int i,int n) which erases n chars starting with the ith one. Remember that the numbering starts at 0.
solution
PS 4
- Write a programme that pushes TFFTT back onto a vector of bools and then makes a second vector whose elements are the negation of the first; print out both.
- Amend the previous programme so that the negation is done in a function.
- Write a programme that asks the user to input a string then creates a vector of chars out of the string.
- Amend the previous programme so that it asks for a seed as well and a string and then encrypts the string by adding rand()%10 to the int of the char, looping back around to 32 if it goes over 126, before converting back into char and printing out the code word. So (input string and seed random number generator)->(convert to vector of chars)->(convert char to int and add a random number between 0 and 9)->(convert back to char, taking 95 away if needed)->(print out)
solution
PS for Reading Week
- Write a programme that asks for a number n and then calculates the Bessel function using some number of terms of the expansion:
J_n(x)=(x/2)^n sum_k=0^infty (-x^2/4)^k / k!(k+n)!
It should be easy to change the number of terms and there should be a function used for the individual terms of the expansion and for the factorial. Output the results for a range of x values and graph the result using gnuplot.
- Write a programme that inverts a 2X2 matrix; check it is invertible first.
solution
PS 5
- Write a programme that creates a random vector of length three and prints it out. The elements of the vector should be pushed back in a function.
- Write a programme that creates two random vectors and compares them to see which is longer, it should print both out and say which is longer, use functions where possible.
- Write a programme that asks for the two components of a 2-vector and an angle and then rotates the vector by that angle. A 2X2 rotation matric has cosines on the diagonals and sine and minus sine on the off diagonals.
solution
PS 6
- The Euler Method is a straighforward though rather inaccurate approach to integrating differential equations. For the equation y_dot=f(y,t) it starts at some initial value y(a)=y_a and update y so that y(t+delta_t)=y(t)+delta_t * f(y(t),t). Write a programme to do Euler integration for the differential equation y_dot=5-y with y(0)=0.
- Along with the value of y in the above programme, print out the real solution, which is y=5-5e^{-t}.
- Write a programme that calculates pi using the power series approximation pi^2/6=sum_{k=1}^infty (1/k^2).
solution
PS 7
- Write a programme that has a pointer to string, print out the address of the original string variable and the value of the pointers, use the * operator to print out the string.
- Use the -> construction to print out the length of the string.
- Make a vector of four strings and use an iterator in a for loop to print them out.
PS 8
- Write a car class; it should have two variables, a bool on and a string noise. It should have default constructor which sets on to false and noise to "vroom" and another constructor which sets on to false and sets the noise to whatever you tell it. It should have a method go() which prints the value of noise iff on is true and a second method switch_on() should set on to true. The programme
#include<cstdlib>
#include "Car.cpp";
using namespace std;
int main()
{
Car vw;
Car audi("Roorr");
vw.switch_on();
vw.go();
audi.go();
}
should produce the output vroom.

by Conor Houghton is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.