Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Section Two Tutorial (Part one)

1. Fibonacci sequence. The program fibonacci.cpp attempts to display


the first 10 numbers in the Fibonacci sequence.
a) Remove the 4 bugs in the program and confirm that the first 10 numbers in the
sequence are 1,1,2,3,5,8,13,21,34,55.
b) Improve the code by changing the variable names, applying suitable whitespace
and adding comments.
c) Add a few statements to the program to display the sum total of the 10 numbers
in the sequence.
d) Modify the program to accept an input value that will decide the number of
values in the sequence. Confirm that the sum total of the sequence of the first 20
Fibonacci numbers is 17710.
e) Run the program to calculate the sum total for the first 50 Fibonacci numbers.
Confirm the correct answer of ≈3.3 * 1010.

2. Write a program that takes the Cartesian components of two vectors


and calculates the dot product, the vector product and the angle (in
degrees) between the two vectors.

3. Olympic Medal Table. The program medals.cpp is an unfinished


program that will read in the medal tally of each country participating
in the 2004 Olympics and display the final medal table ranked
according to the total amount of medals won.
a) Finish the program to produce a medal table sorted by the total amount won.
Comments in the code will guide you as to what needs to be written.
b) Modify the sorting algorithm to produce a medal table ranked by gold medals
won. If the number of gold medals won by two countries are equal, rank by
number of sliver medals, then by bronze medals. Check that your final table is
identical to the official Olympic results.

1
Section Two Tutorial (Part one)
Notes for Question 1
• A number in the Fibonacci sequence is calculated by Fn = Fn -1 + Fn-2.
• The amount of comments and the style of variable names is completely up to
you, there is no wrong answer.
• For (d), consider the fact that every number in the sequence does not have to be
stored in an array for the program to work. Is there a fixed minimum number of
sequence values you can store that will allow you to define the size of the
sequence only when the program is run?
• For (e), consider the type declarations for the variables in the code.

Notes for Question 2


• The equations for this problem should be readily available in any maths or
physics textbook. Otherwise ask me.
• If you really don’t know how to proceed with this question, start by writing a
program to calculate two fixed value vectors and then see how the code can be
modified to solve the general case.

Notes for Question 3


• This question may seem a bit of a challenge at first but this is really only about
the effective use of the bubble sorting algorithm.
• You do not have to worry about the input of the results and the format of the
results table.
• To compile your program you will have to include the provided program
results.cpp in the compilation:
g++ -Wall –o medals.exe medals.cpp results.cpp.
The reason for this will be outlined in Section 3 of the course.
• Ignore any warnings concerning the file results.h
• Use google to find the official results!

You might also like