CPS 006, Spring 2000, Quiz Questions, April 17 - April 24

Here are a few quiz type questions for study use.

Problem 1: What's pointing to what?

Assume the following code fragment is run. What output does it produce?

...
int x = 7;
int y = 25;
int * p = NULL;
int * s = NULL;
p = &x;
s = &y;
*p = 13;
p = &y;
*p = 11;
cout << x << " " << y << " " << *p << " " << *s << endl;
...

Problem 2: Swap Fest

Note the code fragment below.
struct node
{
    int info;
    node * next;
};

node * x = new node;
node * y = new node;
x -> info = 29;
x -> next = y;
y -> info = 15;
y -> next = x;



Write additional lines of code that will cause the swapping of the contents of the two info fields of the nodes created above. (Even though you can see that one node contains a 19 and the other a 15, don't depend on this knowledge. You code must swap the fields even if you do not know the contents.)

Problem 3: Everything in its place.

Write the routine datain which is passed an open file stream indata and an empty vector of pointers to strings named lines. The datain function header is given below.

The function should use the push_back member-function of the tvector class which was covered in lecture. The routine should read in a line at a time (using getline()) and exit when the file runs out of data.

Before exiting, the routine should write both the number of lines read in and the current capacity of lines.

void datain(ifstream & indata, tvector<string *> & lines)
// pre: indata open input stream, lines an empty vector
// post: indata at end of file, lines elements point to strings
//     containing lines from file.
//     number of lines read and capacity of vector have been written to cout
{