EE150 - Spring 1996

Midterm #1 (2/14/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!

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 ``boxes'' model in order to get credit. Put the output to the left of the program and the boxes to the right.

Part A

main()
{
  int m;  int n;   int o;

  m = 1;  n = 10;  o = 5;  p = dostuff(m, n, o);
  printf("%i\n", p);
}

void dostuff(int n, int o, int m)
{
  printf("%i,%i,%i\n", n, o, m);
  return n + o - m;
}

The solution.

Part B

main()
{
  int n;

  n = 1;
  while (n <  16)
  {
    n = n * 2;
    if (n <  8)
      printf("Hello %i\n",  n);
    else
      printf("Goodbye %i\n", n);
  }
}

The solution.

Part C

main()
{
  int i;

  for (i = 5; i >= 0; i = i - 2)
    printf("%i\n", i);
}

The solution.

Part D

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

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

The solution.

------

PROBLEM #2 (24 points, 8 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!

Part A

/* Add up the even numbers between 10 and 20 and print the total */

#include <stdio.h>

int main()
{
  int i;  int sum;

  for (i = 10; i <= 20; i = i + 1)
    sum_of_evens = sum_of_evens + 1;
  printf("%i\n", sum_of_evens);
}

The solution.

Part B

/* Print powers of two backwards from 16 through 2.  That is, its
   output should be:  16 8 4 2 */

#include <stdio.h>

main()
{
  double p;

  p = 16;
  while (p <= 2);
  {
    p = p / 2;
    printf("%i", p);
  }
  return 0;
}

The solution.

Part C

/* Print the smallest value in the input.  For input of: 10 5 3 7 15
   it should print 3. */

#include <stdio.h>
#include <limits.h>

main()
{
  int min;

  min = INT_MAX;  /* A really big number! */
  while (scanf("%i", value) == 0)
    if (value <  min)
       min = value;
    printf("%i", value);
  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 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. ------

The solution.

PROBLEM #4 (26 points)

Assume that the following function has already been written for you:
void drawFilledBox(int h, int w);
This function takes a height (h) and a width (w) and draws a box with that height and width. For example, the call drawFilledBox(3, 5) prints a box 3 lines high and 5 characters wide, like this:
*****
*****
*****
Write a function, drawBoxes, that is passed the maximum size for a box (m) and then draws boxes of size 1 by 1 through m by m. For example, the call drawBoxes(2) produces this output:
*
  
**
**

The solution.

------

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