CS368 Fall 2009

Assignment 2

Due by the beginning of class on Wed. September 23

Please send e-mail questions to your TA: Maheswaran Venkatachalam, kvmakes@cs.wisc.edu

Print out your answers (or hand write them on paper) and turn them in.

  1. What will the contents of array be after the execution of this code fragment? Briefly explain your answer.
       int *p, a[5] = {1,2,3,4,5};
    
       p = a;
       p++;
       p[3] = 0;
    
  2. Is there any error(s) in the following program? If so, identify each error categorize it as a compile time error or a run time error.
    
    #include <iostream>
    
    int main(void) {
       int a[5]={1,2,3,4,5};
       int *b; 
       int i = 0;
    
       while ( i < 5 ) {
          b[i] = a[i] * a[i];
          b++;
          a++;  
          i++;
       }
    
       for (i=0; i < 5; i++)
          cout << a[i];
    
       return 0;
    }
    
  3. Does the following code compile without errors? If yes, what value will get printed? If not, explain why.
    #include <iostream>
    
    int main(void) {
       int a[5] = {1,2,3,4,5};
       cout << a[5];
       return 0;
    }
    
  4. Assume that we have a singly linked list like the one presented in the class lecture notes. This list differs in that it holds characters, instead of integers.
    
    struct Node {
      int value;
      node *next;
    };
    
    Node  *front = NULL;  // Pointer to first element in list.
                          // Initialized to NULL to indicate empty list.
    
    
  5. Suppose we have an array of 25 elements. Each element is a pointer to an integer. Assume that the pointers and integers are already set to initial values.



© 2009 Karen Miller, with questions by Maheswaran Venkatachalam