T÷lvunarfrŠ­i 2

Weekly note 1

The first three weeks of the course will cover C++. As you have already had Java in T÷lvunarfrŠ­i 1 this shouldn't be too difficult for you. On the surface these languages are quite similar, but they differ in many fundamental aspects.

We will spend most of the time getting to know those aspects of C++ that are not in Java or are different from Java. These are things like pointers, arrays, classes, templates and inheritance.

The C++ textbook is specially designed for selfstudy and assumes that the reader hasn't programmed before. Thus we will go very quickly over the first 7 chapters (the first "week"), since you are all expected to have programmed before, even though it was in another programming language.

The book comes with two CDs, one with Visual C++, the other with various material connected to the book. Among that material are exercises that you can use to test your knowledge of each chapter. You should use this to ensure that you have understood the material of each chapter. It is very important that you get a good start in C++, as the rest of the class is heavily based on C++.

The sections on january 11.-12. will be in the computer labs as follows:

Those students that are in the CS student computer lab (i.e. group 2 of T÷lvunarfrŠ­i 2) have to get access to that lab. You have to submit a registration to get that access.

In these first sections we will go through the Visual C++ environment and you will start by going through simple instructions (only in Icelandic!) on the use of Visual C++. Then you should get the file or 4elist0510.cpp, which contains program 5.10 from the textbook, compile it, and run. You are to make a new "Win32 Console Application" and add the program file in by going to "Project | Add to project | Files...". You should try to run the program line by line, by setting up the "Debug toolbar" (right click on toolbar and select Debug). All the programs in the book are online and on CD 1, that comes with the textbook.

Problem set 1

This first problem set is probably the easiest one that you will get in this class. You should therefore really try to do them and put the solution into your section teacher's box before 12:00 monday January 17th. They will not accept solutions that arrive later.

  1. Write a C++ program that accepts as input the integer n and prints out the number 1+2+...+n.

  2. Investigate what happens if you delete the if-part of the fib function in program 5.10 on page 119 in the C++ book. Change the program such that there is no if-statement (drop lines 21 to 26). What is the reason for this new behaviour of the program (apart for you deleting the lines!)?

  3. What is wrong with the following piece of C++ code and what happens when it is run?
         int main()
         {  int i;
            for(i=0; i<10; i++)
                if( i = 0 )
                    cout << "N˙ll" << endl;
                    cout << "Ekki n˙ll" << endl;
            return 0;
  4. What happens when the following program is run and why?
          #include <iostream>
          using namespace std;
          void vixla( int x, int y )
          {   int t = x;
              x = y;
              y = t;
          int main()
          {   int i, j;
              i = 5;
              j = 8;
              vixla(i, j);
              cout << "i=" << i << ", j=" << j << endl;
              return 0;
  5. The square root of the floating point number x can be approximated with the following formula
    ri = (ri-1 + x/ri-1) / 2
    Initially r0 is some positive number larger than 0, i.e. x/2. Write a C++ program that accepts a positive number x and calculates its square root with this method, so that relative error is less than 0.000001. Relative error is the absolute value of (ri - ri-1) / ri.

hh (hja) hi.is, January 10th, 2005.