CS 354, version B
Fall 2008
Name:______ KEY ________
ID:___________________
Exam 1
No electronic devices may be used while taking this exam. Examples of devices not allowed are calculators, pagers, cell phones, wrist calculators/computers, laptop computers, pocket computers. Each student is allowed one 8.5 by 11 inch sheet of paper with handwritten notes. The notes may be on both sides of the paper.

Show all work, and do any/all calculations on the exam. Extra scratch paper may not be used.

Exam Score
Q1 = _____ /   4
Q2 = _____ /   4
Q3 = _____ /   8
Q4 = _____ /   9
Q5 = _____ /   9
Q6 = _____ /   6
Q7 = _____ / 10
Total = _____ / 50



Question 1 (4 points)
A C program foo is invoked by the command line: foo Go 2008 Wisconsin Badgers
What is the value of argc? What is the type of argv[2]?

argc is 5.
The type of argv[2] is the type of *char.



Question 2 (4 points)
What is the purpose of the program counter? How does an instruction like sub d, e, f change value of the program counter?


The PC gives the address in memory of the next instruction to be executed.
This instruction will increment the PC by its length (e.g., 4 bytes).


Question 3 (8 points)
This question is about logical operations, but it is presented in C to be unambiguous.

#include <stdio.h>
#include <stdlib.h>
int main()
{
 unsigned int x = 0xddccbbaa, y = 0x000000ff;
 unsigned int r, s, t, u, v;
 r = x & y;             /* bitwise and */
 s = x | y;             /* bitwise or */
 t = x >> 8;            /* shift right */
 u = ((x && y) == 0);   /* do you remember? */

 ...
}

What are the final values, in hexadecimal, of r, s, t, and u?

 r = 0x000000aa

 s = 0xddccbbff

 t = 0x00ddccbb

 u = 0x0

Question 4 (9 points)
What will this program print? Partial credit will be given only if you draw the six memory locations and get close to the right answer.

#include <stdio.h>
#include <stdlib.h>
int main()
{
 int i, j, k;
 int *p, *q, *r;
 /* initialize */
 i = 4; j = 6; k = 8;
 p = &i; q = &j; r = &k;

 p = q;
 *p  += 2;
 if (*p==*r) *q = 0;

 printf("i=%d, j=%d, k=%d\n", i, j, k);
}



4 0 8





Question 5 (9 points)
Please complete the C code for the recursive function factorial with comments explaining why you do something.

/* F! = F*(F-1)! for F > 0     */
/* 0! = 1                      */
/* Example: 4! = 4*3*2*1 = 24  */

   int   factorial(  int   f)

{


if (  f==0      ) {
     return 1;

}

else {

     return (    f *    factorial(f-1)         );
 }
}









Question 6 (6 points)
Consider the following function that contains at least one error. Please explain the error(s).

/* The strcmp() function compares the two strings str1 and str2.                      */
/* It returns an integer less than, equal to, or greater than zero if str1 is found,  */
/*  respectively, to be less than, to match, or be greater than str2.                 */

int strcmp(char *str1, char *str2)
{
    int i;
    for (i = 0; str1[i] == str2[i]; i++){
        if (str1[i] == '\0')
            return 0;
    }
    return str2[i] - str1[i];
}





Less and greater than are reversed. The last line should be:
return str1[i] - str2[i];


Question 7 (10 points total) Consider 32-bit, two's complement integers A, B, Sum and Diff. Let sign(X) denote the most-significant (sign) bit of X. For example, if A=0x07ffffff then sign(A)==0. Recall that for two's complement integer ADDITION (Sum = A + B), OVERFLOW occurs when:

((sign(A)==0)&&(sign(B)==0)&&(sign(Sum)==1)) || ((sign(A)==1)&&(sign(B)==1)&&(sign(Sum)==0))

(a) (4 points) Assume that A=x7fffffff and B=0x00000001. Does Sum = A + B overflow? Does Diff = A - B overflow?


A = 2^31-1 (largest 32-bit two's complement integer)
B = +1
A + B = 2^31 = 0x80000000 ==> Overflow
A - B = 2^31 - 2 ==> No Overflow




(b) (6 points) If A and B can take any value, write an expression for OVERFLOW with two's complement SUBTRACTION (Diff = A - B):

((sign(A)==0)&&(sign(B)==1)&&(sign(Diff)==1)) || ((sign(A)==1)&&(sign(B)==0)&&(sign(Diff)==0))