You are on page 1of 5

CO1401 - Programming / CO1457

Week 10 – STL: iterators and algorithms

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.

This week I want to start with something different. For this module you need
to be very confident about for loops, arrays (and using arrays with a for loop).
You also need to know about using functions and structures. The first set of
exercises go over these concepts. You must be in the position of being able to
complete these exercises. Ask for help if you do get stuck. You are expected to do
work outside the practicals. Carry on with the exercises and study the lectures
and previous week’s exercises until you are familiar with these concepts.

Use the following check list. Put an X in each check when you feel confident
with the concept. If you are don’t feel confident then carry on with your study until
you do feel confident:
CONCEPT X
for loop
array
using a for loop with an array
writing a function
passing an array to a function
write a structure
write an array of structures
using a for loop with an array
of structures

Programming Exercise 1
 Create a for loop which will count from 0 through to 9. Use cout to output the
value of the index counter.
Programming Exercise 2
 Create a for loop which will count from 5 through to 12. Use cout to output
the value of the index counter.

Programming Exercise 3
 Create a for loop which will count from 15 through to 6. Use cout to output
the value of the index counter.

Programming Exercise 4
 Create an array of integers. The array should be size 6. Initialise the array to
the values: 2, 5, 7, 9, 3, 1.

Programming Exercise 5
 Using the array you created in exercise 4, write a for loop to output the values
of all of the elements of the array.

Programming Exercise 6
 Write a function called “MyFunction”. The function should receive a single
integer in its parameter list. The function should use cout to output the value of the
variable passed to it.
 Invoke “MyFunction” in main sending the value 7 to it.

Programming Exercise 7
 Write a function called “MyArrayFunction”. The function should receive an
array of integers of size 6. The function should use a for loop to output the values
of all of the elements of the array.
 Invoke “MyArrayFunction” using an array. (You can use the array you created
in exercise 4, if you so wish).

Programming Exercise 8
 Define a structure called “SCoordinates”. The structure has two fields: an
integer called x, and an integer field called y.
 Declare a variable of type “SCoordinates” called “myStruct”. Initialise it to
the values 7 and 4.
 Output the value “myStruct” to the screen.

Programming Exercise 9
 Create an array of “SCoordinates” (using the structure definition from
exercise 8). The array should be size 4.
 Initialise it to the values
8, 5
9, 4
7, 3
6, 2
 Write a for loop to output the values of all of the elements of the array, i.e.
four pairs of numbers.

Programming Exercise 10
 Create an empty vector of integers.
 Place the following list of integers into the vector using push_back.
285691
 Use the array syntax to display the contents of the vector. See the lecture notes,
slides 7.

Programming Exercise 11
 Use the vector you created in exercise 10.
 Use an iterator to display the contents of the vector. See the lecture notes, slides
11.

Programming Exercise 12
 Use the vector you created in exercise 10.
 Use an iterator to assign a value of 0 to each of the elements of the vector.
 Display the contents of the vector to check that you did this right.

Programming Exercise 13
 Use the vector you created in exercise 10.
 Use an iterator to output the value the 1st, 3rd and 5th elements of the vector. Hint:
you will need another variable (probably a Boolean) to do this task.
Programming Exercise 14
 Use the structure definition from exercise 8.
 Create an empty vector of “SCoordinates”.
 Assign the following values to the vector:
3, 7
6, 2
7, 3
9, 4
 Write a function which displays all of the elements of a vector of “SCoordinates”.
You must use an iterator to do complete this task.

Programming Exercise 15
 Create a new solution.
 A storage medium such as hard disk is organised as a sequence of clusters, each
being a location on the disk. The locations are numbered in sequence. When a file
is stored on a disk it gets stored in one or more locations. However, the locations
do not necessarily follow one another in sequence. Therefore a list is maintained
of the locations in which successive parts of a file are stored.
 I have included a text file on the elearn called “clusters.txt”. Download it to the
working directory of your program.
 If you examine the file you will see that it contains a list of names each followed
by a sequence of numbers. Imagine that the names give the filenames of files
stored on a hard disk. The associated numbers are the cluster sequences of the
locations on the hard disk in which a file is stored. The number sequence always
ends with a 0.
 Define a structure which will store a string and a vector of integers.
 Declare an empty vector of this structure.
 Do a file read of “clusters.txt”. Remember that you will read a filename from the
file and then read a sequence of numbers. You know that a sequence of numbers
stops when you read in a 0. Do not add the 0 to the vector of clusters – it is just
there to tell you when to stop reading the numbers.
 Store the data in your vector of structures.
 Each element of the vector will store a filename.
 The vector of integers will store the clusters.
 You will end up with a vector of structures (each structure containing filename
and vector of clusters).
 Lastly, use a loop to output the data to the screen.

Advanced Tasks

Programming Exercise 16
 Continue with the previous exercise.
 Write a function to prompt the user for a filename and the display the clusters for
that filename.
 Write a function to search for the shortest list of clusters.
 Write a function to display file fragmentation for a particular filename.
Fragmentation is indicated by how many non-sequential clusters for a file there
are for a given file.

You might also like