Except for Problem #8, you should not use any compiler to run programs or check answers.

Problem #1. Consider the following declaration and initialization of vectors:

vector<int> R; vector<int> S(5, 1); vector<int> T(S);

(i) What are the sizes and the contents of the vectors R, S, T respectively?

R is an empty vector; S is a vector of size 5 with each elements having the value of 1; T is an exact copy of S, size 5 with each elements having the value of 1.

(ii) What is wrong if we then write the following statement:

S[5]=1;

S starts at S[0], so if it has 5 elements it will end at S[4], not S[5].

(iii) Show how you can declare an iterator and use a loop and the begin and the end methods of the vector class to print out the contents of the vector S ?

for (iter = S.begin(); iter < S.end(); iter++)

cout < *iter < endl;

(iv) Show how you can call the insert method to insert the value 0 into S as the first element in S (and therefore increase the total number of elements in S by 1).

S.insert(S.begin(), 0)

Problem #2. What would be the output from the following statements?

int y=10; int & z = y; z = y-1; cout < y < endl < z;

9

9

Problem #3. Consider the following function

void tranform(int x, int & y)

{

x = y*2;

y = x*x;

}

What would be the output from the following code segment?

int i =3, j = 4;transform(i, j); cout < i < endl < j;

3

64

Problem #4. What would be the outputs from the following code segment?

int X[10];

for (int * ptr = X; ptr < X+5; ptr++)

*ptr = 1;

for (int * ptr = X+5; ptr < X+10; ptr++)

*ptr = 0;

ptr = X;

for (int i=0; i< 10; i++)

cout < *(X + i) < endl;

1

1

1

1

1

0

0

0

0

0

Problem #5. [Const; Reference Parameters; Value Parameters; Type Checking]

Given the following function definitions:

void f2(const int & x)

{

x =1;

}

  • f2 would return an error because there is a reference to a constant integer which cannot be changed.

void f4(const int * x)

{

*x =1;

}

This will end in a compile-time error because in the function it is trying to change *x to 1 but it can’t because *x is a constant pointer, pointing to a constant integer.

void f6( int * const x)

{

*x =1;

}

This would run successfully. The address of the pointer x is constant and cannot change, but you can change the value of x like it does in this function: *x = 1;

Do some of them end in compile-time errors? Explain the reason for each case of error.

Problem #6. [Const; Reference Parameters; Value Parameters; Type Checking] Given the following function definitions and the global variable x:

int x=0;

void f1(int x) {

x =1;

}

void f3(int & x) {

x =1;

}

void f5(int * x) {

*x =1;

}

void f6( int * const x) {

*x =1;

}

Consider each of the following function calls as a stand-alone statement immediately following the code segment above. Please indicate whether it will end in a compile-time error or not. If it does, very briefly explain why? If it is fine, explain what is the effect of the statement on the value of x?

f1(0);It will run. When the function starts, x will be initialized at 0, then x will be reset to 1 in the function. The global x will remain unchanged.

f1(x);It will run. The same thing will happen here as it did in f1(0), it will pass 0 (the value of x) to the function, the function will initialize a variable x = 0, that x will change to 1. The global x will remain unchanged.

f3(0); It will end in a compile-time error because the function is looking for the address of a variable, and the number zero isn’t a variable, so it doesn’t really have an address in memory

f3(x);This will work. It will take the address of x and then effectively pass the value 1 to that address, changing the global variable x to 1.

f5(&x);This will work. It passes the address of the global x to the pointer x in the function. It will then set the value 1 at that location in memory, effectively changing the global x to 1.

f6(&x); This will work. x is a constant pointer, but you can still change the value of the pointer like you do here. So effectively, the same thing will happen here as in f5(&x);

Problem #7. [Const qualifier; Type Checking] Given the following declarations of variables

int x1=0; const int x2=0; const int * ptr1=&x1; const int * const ptr2=&x2;

Consider each of the following statements as a stand-alone statement immediately following the code segment above. Please indicate whether it will end in an error or not. If it does, very briefly explain why?

x1=x2;run

x2=x1;error, you can’t change the value of x2 because it is a constant

*ptr1 = 1; error, you can’t change the contents of these pointers.

*ptr2 = 1; error, you can’t change the contents of these pointers.

ptr1 = &x1;run

ptr1=&x2;run

ptr2 = &x1;error, you can’t change the address of this pointer because it is constant

ptr2=&x2;error, you can’t change the address of this pointer because it is constant

ptr2 = ptr1;error, you can’t change the address of ptr2 because it is constant

ptr1 = ptr2;run

Problem #8. (i) Show how you will declare and implement a point class for points on two-D plane using two files: point.handpoint.cpp.

  • The class should have two private integer data members x and y to record the coordinates of the a point object.
  • The class should have a public setCoordinate(int xCord, int yCord) method that will use the two integer arguments to set the integer data members x and y.
  • The class should have a public operator - that will return the Euclidean distance between two points. In other words, if p1 and p2 are two point objects, p1 – p2 should end in the distance between them.

(ii) You should also show how you can set up a file main.cppwith a main function in it such that you can declare two point objects, set their coordinates as you want, and then apply the operator – to determine their distance and print it out.

(iii) Create a C++ project with the files above and compile them to verify that the program can run smoothly without a glitch.

See attached files.

Problem #9~#10: The file list.h describes a linked list class and the file list.cpp provides its implementation in C++. Please answer the questions embedded in list.cpp regarding this linked list class.

9)

i) The value of temp when the loop stops will be NULL

ii)The value of temp when the loop stops in this case will be equal to Val

10)

i)It will not allocate the new temp node

ii)Prev would point to NULL and Curr would point to head

iii)Prev would point to the second to last node and Curr would point to the last node