You are on page 1of 4

CO1401 - Programming

Week 7 – Binary Search

Worksheet Key
A diamond bullet point is an action or a task. Other bullets are information:
 A task to perform
- A point of information

Important In-depth detail Advanced optional task

Always refer to the lecture notes for examples and guidance.


Also look at your previous work, many exercises are similar.

Setting up the C++ development environment and starting a new project


 See the worksheet for week 1 for details about how to do this.

Programming Exercise 1
 Create an structure called SRecord which has two fields:
 mId, which is an integer,
 mName, which is a string.
 It would be good if you can do this task without needing to look anything up.
 However, see the lectures notes for Week 5: slide 18 if you can't remember
how to do this.

Programming Exercise 2
 Create an array called 'records' of SRecord. The size of the array is 15.
 Initialise the array to the values:
2 pete
4 fred
5 john
8 sue
9 mary
10 flo
12 sam
13 max
15 tom
17 jerry
19 kate
20 liz
21 ellie
25 henry
27 oscar

 Again, I'd prefer you to do this without looking anything up but if you need a
reminder then look at the lecture notes for Week 5: slide 20.

Programming Exercise 3
 Write a function that implement a binary search on "records." Use my code as
your starting point - Week 7, slide 17.
 You will need to change the datatype of the array from int to SRecord.

 You do not need to modify the size of the array because it is using the "empty
bracket" notation.
Programming Exercise 4
 Begin a brand new project. In the following exercises you will implement a score
table for a game.
 You will need a new structure and data.
 Create an structure called SPlayer which has two fields:
 mName, which is a string.
 mScore, which is an integer,
 Create an array called 'players' of SPlayer. The size of the array is 9.
 Initialise the array to the values:
albert 7
barbara 3
cuthbert 5
daphne 8
edie 1
francis 0
gertrud 8
e
henry 4
iggy 3

 The table is ordered by name.


 Write a function that implement a binary search on "records." Use my code as
your starting point - Week 7, slide 17. This is just what you did in the previous
exercise.
 You will need to change the datatype of the array from int to SPlayer.
 Write a function to increase the score of a player, given their name and the amount
to increase their score by. In other words, the function is passed the array, a name
and an integer. You search through the array until you find the name. You
increase the score associated with the name by the integer value passed to the
function.

Programming Exercise 5
 Use the binary search algorithm to create a guessing game. Ask the user to think
of a number between 1 and 100. Your program then has to guess the number that
the user is thinking of.
 Your program outputs a number (in effect this is a guess). For the first pass of the
algorithm the guess will be in middle between 0 and 100, i.e. 50. The user then
tells you whether the guess is smaller or larger than the number they are thinking
of.
 Since the user has told you whether the number is smaller or larger, what it has
done is halve the range of possible number, e.g. from an initial range of 0 – 100, it
will now be either 0 – 49 or 51 – 100.
 This is a binary search.
 You can use the existing binary search code as starting point but note that it’s now
just numbers, rather than strings and numbers.

Advanced Tasks
 If you reach the advanced task then I want you to continue looking at classes and
the material from year 2.
 You should have access to CO2402: Advanced C++ module. A version of this has
been zipped up on module page.
 Look at the lecture from week 5 of the CO2402 module: “Introduction to classes”.
 Go through the worksheet from week 5.
 If you finish this then carry on with any of the material from CO2402.
 If you need help then ask your practical tutor or ask Gareth

You might also like