CS 1005, C Term 2000
Introduction to Programming
Solutions to Test 3

Your Name: _____________________________________

Your Lab Section: ______________     Your Login Name:_________________

Instructions.   Read each problem carefully before answering. Circle the selected answer(s) or write your solutions in ink in the spaces provided when this is the appropriate action as indicated below for each problem. Write neatly. Each problem is worth 60 points. All parts of each problem have equal point value. C++ is the programming language used throughout this test. Good luck!

  1. Assume that the following object declarations are in effect. No memory allocation should be assumed except as indicated explicitly below.
    
       char *p = new char[10];
       char *q; // note that no memory allocation for q is provided
    
    

    a) Circle all statements below that could lead to an error (at either compile time or run time) if used within the scope of the above declarations. All statements that would not lead to errors should remain uncircled.

    
       *p = *q;                             p = new Dice(6); <--
    -->*q = 'a';                            *p = q;   <--
       *p = 'A';                            cout << p[7];
    
    

    b) Give the output of the following program segment:

    
       q = new char[5];
       for (int k=0; k<5; k++) {
          q[k] = char( int('A') + k );
       }
       for (int k=0; k<5; k++) {
          cout << q[4-k] << " ";            
       }
    
    
       Output: E D C B A
    
    
    
    

  2. For each of the following, write a program segment that accomplishes the stated objectives. All variables used must be declared unless otherwise stated.

    a) Declare an output file stream named datafile.

       ofstream datafile;
    
    

    b) Bind the output file stream named datafile to the file whose name is contained in the string class object named filename. You do not need to declare any objects.

       datafile.open(filename.c_str());
    
    

    c) Insert an object named coord of type double into the datafile stream. No declaration for datafile is needed. All other objects should be declared.

       double coord;
       datafile << coord;
    
    

    d) Read from a file named "chars" one character at a time (do not skip whitespace), place each character in an object named c, and write c to datafile whenever c represents a letter of the alphabet. Any necessary stream objects associated with the input file must be explicitly declared and appropriately prepared for reading.

       ifstream letterfile;
       letterfile.open("chars");
       char c;
       while (letterfile.get(c)) {
          if (isalpha(c)) {
             datafile.put(c);
          }
       }
    
    
    
    

  3. In each of the following cases, write a suitable header for the function described. Arrays should be passed as pointers.

  4. The third statement in the program segment shown below calls a generic function named f. The actual output produced by the final statement depends on the definition of f, which is not shown.
    
       double fnum = -1.2;
       double *fptr = &fnum;
       f(fptr);
       cout << *fptr;    // actual output depends on the definition of f
    
    
    For each of the following alternative definitions for the function f, state what would be printed by the final statement in the above program segment if that particular definition of f were used. Any given program would of course include at most one of these possible definitions for f.
    
    b) // 1st definition of f
       void f(double* x) {           Answer for b): -1.2
          double *y;
          y = new double;
          *y = 5.9;
          x = y;
       }
       
    
    
    a) // 2nd definition of f 
       void f(double*& x) {          Answer for a): 5.9
          double *y;
          y = new double;
          *y = 5.9;
          x = y;
       }
       
    
       
    c) // 3rd definition of f
       void f(double* x) {           Answer for c): 5.9
          double *y;
          y = new double;
          *y = 5.9;
          *x = *y;
       }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

  5. Assume the following definition of the Node type. The typedef for the Item type should be filled in before compilation. Any specific assumptions about the Item type are provided in each part of this problem below.
       struct Node {
          typedef ___ Item;
          Item data;
          Node* link;
       };
    
    a) Fill in the blanks in the loop test and body of the while loop below so that when the resulting program segment has executed, cursor points to the first Node of the linked list with head pointer head_ptr that contains value in its data member.
       Node* cursor = head_ptr;
       while ((cursor != NULL) && ((cursor->data)!=value)) {
            cursor = cursor->link;
       }
    
    

    b) Write the output produced by the following program segment next to the word "Answer" below, or write "SEGMENTATION FAULT" if the program attempts to access unallocated memory (but show all partial output produced before any such illegal access). Assume that the constructor for the Item type initializes all Item objects as 0.

       Node *head_ptr, *cursor;
       head_ptr = new Node;
       head_ptr->data = 6;
       head_ptr->link = new Node;
       cursor = head_ptr->link;
       cursor = cursor->link;
       cout << cursor->data << endl;
    
       Answer: SEGMENTATION FAULT
    
    

    c) As in b), but for the following program segment. Assume that the Item type is int in this context.

    
       Node *head_ptr, *cursor;
       head_ptr = new Node;
       cursor = head_ptr;
       for (size_t i=0; i<5; i++) {
          cursor->data = i;
          cursor->link = new Node;
          cursor = cursor->link;
       }
       cursor = head_ptr;
       for (size_t i=0; i<5; i++) {
          cout << cursor->data << " ";
          cursor = cursor->link;
       }
    
       Answer: 0 1 2 3 4