Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

Problem 1

If you need to fetch the element 1/3rd of the way into an array range i..j frequently in a program, you might try a macro
#define THIRD(A,i,j) A[i + (j-i)/3]
    
or you might try an inline function
inline int third(int *A, int i, int j) { return A[i + (j-i)/3]; }      
    
  1. What's the "inline" do?
  2. Why does THIRD(array,1+2,9) give the wrong element of array? Show how to fix the definition of THIRD.
  3. Why is it "THIRD(foo,a,b) = 4" will compile, but not "third(foo,a,b) = 4"?

Problem 2

Why can't you make a macro out of:
int gcd(int u, int v)
{
  if (v == 0) 
    return u;
  return gcd(v,u%v);
}

Problem 3

Consider the following program in C-ish syntax:
int foo(int a, int b)
{
  printf("in foo!\n");
  if (a != 0)
    return a;
  else
    return b;
}

int bar(int x) { return 100/x; }

int main()
{
  int m, n;
  scanf("%d %d",&m,&n);
  printf("%d\n",foo(bar(m),bar(n)));
  return 0;
}
Assuming the user enters 101 followed by 0,
  1. what would the output of the program be if applicative order is used? Justify!
  2. what would the output of the program be if normal order is used? Justify!