CS 61C (Fall 2007)

Quiz 2

Two questions, submit as "quiz2".  Due 2:45pm before lecture 8/31/2007.


1.

Consider the following program.
#include <stdio.h>

void swap (int* px, int *py) {
    int* temp;
    *temp = *px;
    *px = *py;
    *py = *temp;
}

int main ( ) {
    int a = 3, b = 7;
    swap (&a, &b);
    printf ("a's new value is %d; b's new value is %d\n", a, b);
    return 0;
}
Which of the following is true?
  1. The program doesn't compile.
  2. The program compiles, but when run, it crashes.
  3. The program compiles. When run, it prints a's new value is 3; b's new value is 7
  4. The program compiles. When run, it prints a's new value is 3; b's new value is 3
  5. The program compiles. When run, it prints a's new value is 7; b's new value is 3
  6. The program compiles. When run, it prints a's new value is 7; b's new value is 7
Briefly explain your answer.



2.

Specify how to fill in the blank in the program segment below so that the call to assign has the effect of storing x's value into values[0] Do not use any brackets—"[" or "]"—in your solution.
int main ( )
    int values[20];
    int x;
      ...
    assign ( _____ , x);
      ...
}

void assign ( int *y , int x) {
    *y = x;
}

Briefly explain your answer.