EE150 - Spring 1996

Midterm #2
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


int table[8] = {87, -1, 94, 0, 80, 1, 90, 70};

main()
{
  int i; 

  for (i = 7; i >= 0; i--)
  {
    if (table[i] > 75)
      printf("%i\n", table[i]);
    if (table[i] <= 0)
      break;
  }
}

The solution.

Part B

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

  for (i = 0; i < 6; i++)
  {
    if (i >= 3 && i <= 4)
      printf("%i-a\n", i);
    if (i >= 3 || i <= 4)
      printf("%i-b\n", i);
  }  
}

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 4 mistakes.

Part A

/* 
 * Print the number of digits found in this program's input.
 */
#include <stdio.h>

main()
{
  int count;
  int c;
  
  c = getchar();
  while (c != EOF)
    if (isdigit(c) == 0)
      count = c + 1;
  printf("Found %i digits in input\n", count);
  return 0;
}

The solution.

Part B

/* 
 * Count the number of negative values in an array "table" that
 * contains "n" elements.  Return this count.
 */

int count_negatives(int table[], int n)
{
  int count;
  
  count = 0;
  for (i = n - 1; i > 0; i--)
    if (a[i] > 0)
      count++;
  return i;
}

The solution.

------

Problem #3

Write a program that takes a barchart as input and prints the length of each bar. At the end, it prints the number of bars and the average length of each bar. For example, here's its I/O.
#######
7
###
3
############
12
control-D
There were 3 bars with an average length of 7.

The solution.

------

Problem #4

Write a function, maxvalue that returns the largest value in an array of n integers. That is, the function's prototype looks like:
int maxvalue(int table[], int n);
You may assume that n will be at least 1.

The solution.

------

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