MCS 275 midterm #1 19971003 D. J. Bernstein

Problem 1. Write a function biglittle so that after

int x; int y; int big; int little;

the code

biglittle(x,y,&big,&little);

has the same effect as

if (x<y) { big=y; little=x; }
else { big=x; little=y; }

Problem 2. What does this program print?

#include <stdio.h>
void doit(int p[],int n)
{
  int t;
  while (n > 0) {
    t = p[0];
    --n;
    p[0] = p[n];
    p[n] = t;
    ++p;
    --n;
  }
}
int x[5] = {3,1,4,1,5};
main()
{
  int i;
  doit(x,5);
  for (i = 0;i < 5;++i)
    printf("%d\n",x[i]);
  doit(x,5);
  for (i = 0;i < 5;++i)
    printf("%d\n",x[i]);
}

Problem 3. Write a program that (1) reads numbers into an array, (2) prints all the negative numbers in the array, and then (3) prints all the positive numbers in the array. You may assume that there are at most 100 numbers. Do not ask the user in advance how many numbers there are.

Problem 4. What does this program print?

#include <stdio.h>
struct t { int a; int b; } ;
struct t x[5] = {
  { 3, 4 }
, { 2, 0 }
, { 4, 3 }
, { 1, 1 }
, { 0, 2 }
} ;
main()
{
  int i = 0;
  do {
    i = x[i].a;
    printf("%d\n",x[i].b);
  } while (i);
}

Extra credit. Write a function ouch, using exactly one variable, so that

printf("%d\n",***ouch());

prints 27182.