Sample Questions

These two questions are illustrations of the type of question that will be in Section A (short questions) of the exam. Each question would be worth 10 marks (out of 100 in the real exam).

You should be able to complete these questions in less than fifteen minutes each.

Question 1

A vector of dimension n is an n-sized array of real numbers. If (a0,a1,...,an-1) and (b0,b1,...,bn-1) are vectors with the same dimension, their dot product is the single number
a0b0 + a1b1 + ... + an-1bn-1.

In the file dotprod.c, you will find a function dotprod(), together with some main code to read arrays and print the result. Complete the function dotprod() so that it computes the dot product of a and b of dimension n.

Question 2

The following table is a simplified extract from the sentencing guidelines for speeding offences:

Speed limitRecorded speed
3031-4041-5051-
4041-5556-6566-
Penalty3 points4-6 pointsdisqualify

In the file speeding.c, you will find a main routine that reads a speed limit and recorded speed. Complete the program so that it prints out the penalty (exactly as described in the table).


This question is an example of a section B Descartes question.

Question 3

In this question, we will develop the beginnings of a simple interactive drawing tool. The program works in two phases: first, it reads a series of points from the user (by clicking), and marks them on the screen. In the second phase, the user can draw lines between the given points, by clicking on the start and end points.

You are provided with a template file drawing.c. Study the code and comments in this file.

Recall that to compile this program you should do
gcc drawing.c descartes.o -lSDL

(a) Your first task is to complete the routine void MarkPoint(point_t p), which marks the point on the screen. If the argument point p is at (x,y), then your code should draw a square (by constructing and drawing line segments) whose corners are (x ± c, y ± c), where c is the value of the program variable OFFSET. (5 marks)

(b) The second task is to complete the routine int FindPoint(point_t p). Given the argument p, your code should return the index i of the first point q = points[i] such that p is within OFFSET units of q in both coordinates. That is, qx − OFFSET ≤ px ≤ qx + OFFSET and similarly for the y coordinate. If there is no such point, your code should return -1. (10 marks)

(c) The final task is to complete the main routine. In the section marked for the answer to part (c), add code to do the following: use AskPoint to get the end point index from the user; clear the selection marker on the start point (by calling MarkSelected with second argument 0); set the finished flag if the user middle-clicked, and otherwise draw a line from the start point to the end point. (10 marks)


Home : Teaching : Courses : Cp 

Informatics Forum, 10 Crichton Street, Edinburgh, EH8 9AB, Scotland, UK
Tel: +44 131 651 5661, Fax: +44 131 651 1426, E-mail: school-office@inf.ed.ac.uk
Please contact our webadmin with any comments or corrections. Logging and Cookies
Unless explicitly stated otherwise, all material is copyright © The University of Edinburgh