Tölvunarfræði 2
Weekly notes 2
Last week we quickly covered the material from the chapters 1-5 and 7
in the C++ book. To ensure that you have not missed anything
important you should utilize the exams on the first CD that comes
with the book.
This week we will start on pointers, references, and classes. This is from
chapters 8, 9, and 6 in the book. Next week we will then cover chapter 10 that
looks at some more complex issues on member functions (methods).
It has been my experience that students have the most difficulty with
understanding pointers in C++. Pointers will be used a lot in this
class and you will have problems if you do not understand them.
Problem set 2
Solve the following problems and put the solutions into your section teacher's box
before 12:00 monday jan. 19th. They will not accept solutions that arrive later.
- Wrtie a C++ program to calculate the number sequence f that has
f(1) = f(2) = 1, but in general
f(n) = f(n - f(n-1) - 1) + f(n - f(n-2) - 1)
Print out the first few numbers in the sequence.
- Write the function string Sesar( string inn, int k ) that codes the string
inn Caesar cipher and returns a string with the coded version.
In Caesar cipher the alphabet is shifted by k (0 ≤ k < 26) characters
(we will only use english capital letters to simplify the program). If k is 2 then
the letter 'A' becomes the letter 'C', the letter 'R' becomes 'T', etc. Note that this wraps
around, so that the letter 'Z' becomes 'B'.
Also write a main program that allows the user to enter a string with a key k and then
it writes out the codes string. To work with string you have to include the library
string.
- Given a C++ program that defines the int-pointer p and assigns it the
value 20. What happens when you execute the statement "*p = 2;" and why? Explain.
- Memory location that a program gets using the statement new is returned with
the statement delete. If the program ask again for a memory location it is likely
that it will be assigned the location it just returned. Investigate if this is the case
by checking the address of the memory location that the program gets. Also check whether
values that the program stores in the locations are still there if it gets the same location.
- Consider the following program segment:
double x = 2.0;
int *p = new int;
double *q = &x;
*p = *q;
x = *p**q;
Show the status of each variable using a drawing. What is the value of each variable?
Those that want more practice should try solving the following problems. Their solutions
are all available, either on an old homepage of the course or at the back of the C++
textbook. If you want an explanation of the solutions you can ask about them in your section.
- Problems 1 and 2 on weekly note 2
from last year.
- Problem 1 on weekly note 2
from spring 2003.
- Problems 4 and 5 in Day 4 on page 92 in the C++ textbook.
- Problem 5 in Day 5 on page 128 in the C++ textbook.
- Problem 6 in Day 7 on page 198 in the C++ textbook.
- Problem 6 in Day 8 on page 242 in the C++ textbook.
hh (hja) hi.is, January 17th, 2005.