Sample paper: pdf, short pdf, source, solution: pdf, short pdf, source.
Outline solution for 11-12 papge: pdf, short pdf.
Printable versions: PS1 PS2 PS3 PS4 PS5 PS reading week PS6 PS7 PS8.
PS 1
A short problem sheet this week!
- In a terminal type emacs Hello.cpp & to open a new file and type into it the hello world programme:
#include
#include
using namespace std;
int main()
{
cout<<"Someone must have been spreading false rumors"<<endl;
}
Save it using ^x^s and in the terminal window compile it using g++ Hello.cpp and run it using ./a.out. If you would prefer to use a different editor, that's fine. You can close emacs with ^x^c. Play around with emacs and the programe a bit, for example, try
cout<<"Someone must have been spreading false rumors\n";
instead. What is the role of the \n?
PS 2
- Write a program to investigate what a/b is if a=7 and b=3 for the four possibilities of a and b being ints and doubles.
- What do you get if your add to chars? What is 'a' plus 'b'?
- Can you do a++ is a is a char?
- What value do you get if declare an int but don't initialize it? What about a double or a float?
Submit the program you wrote to answer problem 3 and send it to tcd.ma1262@gmail.com both by cut and pasting it and as an attachment. Say if it compiles and put PS2 and your tutorial time in the subject line.
PS 3
- 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.
Submit the program you wrote to answer problem 4 and send it to tcd.ma1262@gmail.com both by cut and pasting it and as an attachment. Say if it compiles and put PS3 and your tutorial time in the subject line.
solution
PS 4
- 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.
Submit the program you wrote to answer problem 3 and send it to tcd.ma1262@gmail.com both by cut and pasting it and as an attachment. Say if it compiles and put PS4 and your tutorial time in the subject line
solution
PS 5
- 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.
The problems this week are quite long and so they won't be marked, if you would like one of your answers checked, send it in the usual way.
PS for Reading Week
- Write a programme with a function that calculates x^n/n!, you shouldn't calculate x^n and n! separately because n! quickly becomes too large for int, instead you should multiply x/r for r from one to n. Use this function in a programme that calculates sine and cosine using the power series. Work out how accurate these approximations are at pi/2 for different values of n. The trigonometric function are in the cmath package and the easiest way to get pi is to write double pi=4*atan(1), atan() gives the arc tan.
PS 6
- 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 to have it make a new vector with the characters in the other order; print out the original string backwards.
Submit the program you wrote to answer problem 1 and send it to tcd.ma1262@gmail.com both by cut and pasting it and as an attachment. Say if it compiles and put PS6 and your tutorial time in the subject line.
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.
Submit the program you wrote to answer problem 1 and send it to tcd.ma1262@gmail.com both by cut and pasting it and as an attachment. Say if it compiles and put PS7 and your tutorial time in the subject line.
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.
Submit the program you wrote and send it to tcd.ma1262@gmail.com both by cut and pasting it and as an attachment. Say if it compiles and put PS8 and your tutorial time in the subject line.

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