Answers to Sample Final


  1. Returns a pointer the the last occurance of c in s.
    1. 0x4050
    2. illegal assignment
    3. 0x1010
    4. illegal assignment
    5. 'f'
    6. 'h'
    7. 'r'
    8. yes
    9. yes
  2. It prints the character 'B', then a newline, then the character 'C', then another newline.
  3. The argument to a_again is passed by copy, not by reference. You need to pass a pointer:
    	void a_again(int *acount)
    	{
    	        ++*acount;
           	}
    
    	void main(void)
    	{
    	        register int c;
    	        int counter = 0;
    
    	        while((c = getchar()) != EOF)
    	                if (c == 'a' || c == 'A')
    	                        a_again(&counter);
    
    	        printf("%d\n", counter);
    	        exit(0);
    	}
    
  4. Here is one solution.
    	int dot( int a[], int b[], int n )
    	{
    		int i;
    		int sum = 0;
    
    		for(i = 0; i < n; i++)
    			sum += a[i] * b[i];
    		return(sum);
    	}
    
    A good practise exercise is to write a recursive version of this routine. (Hint: the base case is where n = 1, becauseyou return the product of the single elements in the vectors a and b.
  5. The problem is that the order of evaluation of function arguments is undefined, so the call f(++i, ++i) could be either f(6, 7) or f(7, 6). Hence the two possible answers are
    	6 7
    
    or
    	7 6
    
  6. Here is one solution:
    	int sum(int a, int b)
    	{
    	  return(a == b ? a : (a + sum(a+1, b)));
    	}
    
    1. This adds the even numbers from 0 to 102 inclusive.
    2. No. x is 0 since the while condition is initially false.
    3. No. This adds the odd numbers from 0 to 100 inclusive.
  7. This function emulates strcmp. It returns the numerical difference between the first mismatched character in strings s and t; it returns 0 if the strings are the same.
  8. Three problems are:
    1. The c to which the value of getchar() is assigned is declared as a char rather than an int. Now, EOF is the integer -1; so when it is assigned to a character, only the low 8 bits are used. When c is compared to EOF, c's value is changed to an integer. If c is signed, it becomes -1; if c is unsigned, it becomes 255 (because the 8 low-order bits are the same as the contents of c, and the high-order 24 bits are 0). In the former case, the comparison in the while loop fails and the loop ends. In the latter case, the comparison succeeds (as 255 != -1) and the loop never terminates. As char without any modifier may be either signed or unsigned (it is implementation dependent), to fix this problem either c must be declared as unsigned char c or (better) as int c.
    2. The return value of malloc() is not checked. The check should be added.
    3. Adding two pointers is not legal in ANSI and is inherently non-portable. The expression (buf + p) / 2 should be buf + (p - buf) / 2.

Send email to cs40@csif.cs.ucdavis.edu.

Department of Computer Science
University of California at Davis
Davis, CA 95616-8562



Page last modified on 12/4/97