CS140 -- Final Exam: Questions 1 - 3

December 15, 1998

Questions 1-3 pertain to the following program, which takes an integer n on the command line.
main(int argc, char **argv)
{
  int n, i, k;
  int total;

  if (argc != 2) exit(1);
  n = atoi(argv[1]);
  if (n < 0) exit(1);

  total = -20;

  for (i = 0; i <= n; i++) {
    total++;
    for (k = n; k > 1; k = k / 2) {
      total++;
    }
  }
  printf("%d\n", total);
}

Question 1 (2 points)

What does the program print out when n is 4? Write your answer on the answer sheet.


Question 2 (4 points)

Exactly what does the program print out as a function of n? (e.g. it prints out 2n, or 5n*n - 40). If you want, you may assume that n is a power of two.

Question 3 (9 points)

Look at the table on the answer sheet.

Let the output of the program be f(n).

Each column of the table is a different function g(n). There are four rows, one each for O(g(n)), Omega(g(n)), Theta(g(n)), and o(g(n)). Your job is to put an X in the each entry of the table where g(n) is has the proper relationship to f(n).

For example, consider the first column, where g(n) = 1.

  • Put an X in the first row of that column if you believe f(n) = O(1).
  • Put an X in the second row of that column if you believe f(n) = Omega(1).
  • Put an X in the third row of that column if you believe f(n) = Theta(1).
  • Put an X in the fourth row of that column if you believe f(n) = Theta(1).
Fill in all entries of the table in this way.