CS 1005, C Term 2000
Introduction to Programming
Practice Problems for Test 3

  1. 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 input file stream named datafile.

       
    

    b) Bind the input file stream named datafile to the file whose name is contained in the string class object named filename.

       
    

    c) Place the next int object from datafile into an object named number.

       
       
    

    d) Read datafile one character at a time, placing each character in an object named c, and printing c to a file named "lettersonly" whenever c represents a letter of the alphabet. Any necessary stream objects associated with the output file must be explicitly declared and appropriately prepared for writing.

       
       
       
       
          
             
          
       
    

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

  3. 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 = 3.14;
       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.
    
    a) // 1st definition of f 
       void f(double*& x) {          Answer for a): 
          double *y;
          y = new double;
          *y = 1.99;
          x = y;
       }
       
    
       
    b) // 2nd definition of f
       void f(double* x) {           Answer for b): 
          double *y;
          y = new double;
          *y = 1.99;
          x = y;
       }
       
    
    
    c) // 3rd definition of f
       void f(double* x) {           Answer for c): 
          double *y;
          y = new double;
          *y = 1.99;
          *x = *y;
       }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

  4. Assume the following definition of the Node type:
    
       struct Node {
          typedef ___ Item;
          Item data;
          Node* link;
       };
    
    
    a) Fill in the blanks in the for statement below so that the resulting program segment leaves the correct length of the linked list with head pointer head_ptr in the variable named length.
    
       Node* cursor;
       size_t length = 0;
       for (cursor = __________; cursor != __________; cursor = ____________)
          length++;
    
    

    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 = 5;
       head_ptr->link = new Node;
       cursor = head_ptr;
       cout << cursor->data << endl;
    
       Answer: 
    
    

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

    
       Node *head_ptr, *cursor;
       head_ptr = new Node;
       cursor = head_ptr;
       for (size_t i=0; i<10; i++) {
          cursor->data = i;
          cursor->link = new Node;
          cursor = cursor->link;
       }
       for (size_t i=0; i<10; i++) {
          cout << cursor->data << endl;
          cursor = cursor->link;
       }
    
       Answer: