EE150 - Spring 1996

Midterm #1
Sample Exam

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

Here are some good questions taken from past EE150 midterms that you can use as practice for the current midterm. A real EE150 midterm is longer than this sample exam, so you should shoot for being able to do these sample questions in under 40 minutes.

------

Problem #1

What do each of the following main programs print? Show the output BELOW the program in the space provided. Show your work to the right of the program using the "boxes" approach discussed in class. You get no points if you fail to draw boxes for each variable and show us exactly how its value changes.

Part A

main()
{
  int i;  int n;

  i = 1;  n = 2;
  while (i <  10)
  {
    i = i * n;
    n = n + 1;
    printf("%i\n", i);
  }
}

The solution.

Part B

main()
{
  int weird(int x, int y);

  int a;  int b;  int c;

  a = 10;  b = 6;   c = weird(a, b);
  printf("%i %i %i\n", a, b, c);
}

int weird(int b, int c)
{
  int a;

  c = c / 2;
  b = b - 1;
  a = b * c;
  
  printf("%i %i %i\n", a, b, c);
  return a - c;
}

The solution.

------

Problem #2

Show us the minimum set of changes that we must make to each of these programs to get them to compile successfully and run exactly as their comment describes. That is, you should show us what we have to add, delete, or replace to make these fucntions work! HINT: Each function has at least 4 mistakes. NOTE: Assume all of these programs have the necessary #include for stdio.h even though to save space we haven't shown it.

Part A

/* 
 * Read a person's age (an integer) and print a message that
 * indicates whether that age is old enough to drink legally
 * (21 or over).
 */
#define MIN_AGE 21;

main()
{
  scanf("%i", x);
  if (x != MIN_AGE)
   printf("%i is old enough to drink\n", x);
  printf("%i is too young to drink\n", x);
  return 0;
}

The solution.

Part B

/* 
 * Use a function to compute the value of one purchase using a
 * 10% sales tax rate.  If the input is 4.50, then the output
 * should be 4.95.
 */

main()
{
  double amount;
  
  scanf("%lf", &amount);
  printf("%f\n", compute_cost(amount, 10.0));
}

void compute_cost(int purchase_amount, double sales_tax)
{
  return purchase_amount + purchase_amount * sales_tax;
}

The solution.

------

Problem #3

Write a function, draw_triangle, that is passed a single value, x, and prints an upside-down right triangle that has both a base and height and that is x characters wide. The function should then return the total number of characters it wrote. For example, the call:
n = draw_triangle(6);
would result in this output
$$$$$$
$$$$$
$$$$
$$$
$$
$
and would place a 21 into n. HINT: Your function should not take more than 10-12 instructions.

The solution.

------

Problem #4

Write a program to read a series of input values and print the largest input value. For example, if the input is:
20 40 60 10 15 80 12 50 30 12
your program's output should be:
The largest input value is 80.

The solution.

------

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