EE150 - Fall 1996

Midterm #1 (10/2/96)

Jump To Problem: [1 | 2 | 3 | 4]
------

This exam is a total of 6 pages, including this one. It is open book, open notes, and closed neighbor! Unless noted, write your answers on the blank pages at the end of the exam. Hand in all exam papers given you.

You have 50 minutes to complete the exam. We will pick up all midterms and leave the room at 55 minutes after the hour.

There are 4 problems worth a total of 100 points. The problems are almost equally weighted. Be careful not to spend too much time on any particular problem. If you get stuck, go on to the next problem. You can always come back to the one that is giving you problems. As a guideline, allow about 12 minutes per problem, which leaves you a couple of minutes at the end to check your work. To ensure you have read these instructions, you get two bonus points simply for writing your account name on the bottom of the last page of this exam. ------

PROBLEM #1 (24 points, 6 points each)

Tell us what each of the following program fragments do. To determine the answer, work through the code by hand and show us the output that results. You must show how these programs execute with the ``tables'' model discussed in class in order to get credit. Hint: A table method of showing the output involves showing the time or execution steps on one axis (say the horizontal axis) and each variable that changes on the other axis (say the vertical axis). Hint 2: It wouldn't hurt, after the table, to explain your reasoning if you feel your table is not clear.

Part A

main()
{
  int a;  int b;   int c;   int p;
  int dostuff(int,int,int);

  a = 1;  b = a * 3;  c = b - a + 5;  p = dostuff(a, b, c);
  printf("xx: %i\n", p);
  a = b + p;
  printf("xx: %i\n", a);
  printf("xx: %i\n", b);
}

int dostuff(int c, int a, int b)
{
  printf("yy: %i,%i,%i\n", a, b, c);
  return a + b * c;
}

The solution.

Part B

main()
{
  double n;
  int m; int p;

  n = 96;
  while (n >  13)
  {
    n = n / 2;
    if (n <  35)
      printf("Okay %4.1f\n",  n);
    else
      printf("No way %5.2f\n", n);
  }
m = n + 1 + 0.51;
printf("O vay %i\n", n);
}

The solution.

Part C

main()
{
  int i;
  double b;
  int c;  int j;
  int change (int);
  
  b = (int) (13.4 + 12.54);
  c = b * 0.5;  
  j = c - 5;

  printf("b=%4.2f, c=%i, j=%i\n", b,c,j);

  b = change( j );

  printf("b=%4.2f, j=%i\n", b,j);

  for (i = c; i >= 0; i = i - j)
    printf("lp i=%i\n", i);

  printf("dn i=%i\n", i);
}

int change ( int j )
{ 
  j = j * 2; 
  return (int) (j * 2.0); 
}

The solution.

Part D

main()
{
  int m; int q;
  q = m;
  printf("%i\n", q);

  m = 5 + 2 * 3 + 2;
  m = 5 * 2 - 3 * 2;
  m = (q == m) + 10;
  printf("%i\n", m);
}

The solution.

------

PROBLEM #2 (24 points, 12 points each)

Each of the following programs have a comment that says what they are supposed to do. But they don't do what they're supposed to do! Show us the minimal set of changes necessary to make these programs do what their comment claims that they do. That is, mark up the programs to make them work!

Note that errors can either by syntactic (that is, problems with the format of the C code, or semantic (that is, problem with how the C code meets the problem requirements.

Part A

/* 
   Sum two times each of the even numbers between START and FINISH together
   with three times each of the odd numbers between START and FINISH

   Important Hint: #define A B results in a replacement of any A
   with B before the program is compiled.
*/

#include <stdio.h>

#define START  5
#define FINISH 8
#define T2 thrice
#define T1 twice

int main()
{
  int i;  
  int sum_even;  
  int sum_odd;

  int even(int x);
  int twice(int x);                       

  for (i = START; i <= FINISH; i = i + 1)
    {
      if (even(i))
          sum_even = sum_even + T2(i);
      else
          sum_odd  = sum_odd  + T1(i);
    }
  total = sum_even + sum_odd;
  printf("%i\n", total);
}

int even(int x)
{
  /* This function determines whether a number is odd or even
     by dividing by two - if there is a remainder, the number
     must be odd */

  double ck;
  ck =  (double) (x / 2.0) - (int) (x / 2);
  if (ck > 0.0) 
    return 0;
  else return 1;
} 

int odd(int x)
{
  /* A simpler way to determine even/odd */
  return ((x % 2) == 1);
} 

int twice ( int x )
{ 
  return 2 * x; 
}

int thrice ( int x )
{ 
  int twice(int x);
  return twice(x) + x;
}

The solution.

Part B

/* Print powers of 2 forwards from 0 through FINISH followed by
   the total.  That is, for FINISH = 7,  output should be: 
   1 2 4 8 16 32 64 128 = 255 */ 

#include <stdio.h>

#define FINISH 7

main()
{
  double count; 
  double p;
  double total;

  total = 0;                             
  count = 0;

  p = 1;
  while (p <<= FINISH) 
  {
    printf("%i ", p);
    total = total + p;
    p = p + 2;     
    count = count + 1;
  }

  printf(" = %i\n", count); 
  return 0;
}

The solution.

------

PROBLEM #3 (26 points)

Write a program that reads a set of scores and prints the percentage of the class that got A's, where an A is 90 or better, as well as the total number of A's and the total number of students. For example, if the input is:

80 90 57.0 95 30 75
the output should be:
33% of the students (2 out of 6) got A's.
Do not worry about handling input errors, but you MUST use at least a function in your solution that has a prototype that looks like:
int classify_got_A( double grade );
------

The solution.

PROBLEM #4 (26 points: 13 x 2)

Imagine a function like the following ...
int Fbig(int i)
This function takes an integer (i) and returns an integer. Fbig(1) = 1 means that Fbig returns 1 if it is passed a one. Similarly, Fbig(2) = 2, Fbig(3) = 6, Fbig(4) = 24, Fbig(5) = 120. Fbig is a very simple function to define. Assume that Fbig is defined for you. Write a function, inputValues, that inputs integers from the standard input until an EOF (ctrl-d) is input, and prints out the Fbig value in a statement like:
Fbig of x is y.
where y = Fbig(x).

If you get done the first part, write a version of the function Fbig. Hint: you might be able to write it in about 4 lines. Hint 2: Fbig(n) = n * n-1 * ... * 1.

The solution.

------

[EE150 Home Page | EE150 Exam Information Page | Top Of Page]