EE150 - Spring 1996

Midterm #2 (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 initials at the bottom of this page. ------

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 c, s = 0;

  for (c = 1; s < 6; c++)
    s += c;
  printf("c=%i, s=%i\n", c, s);
  return 0;
}

The solution.

Part B

main()
{
  int i;

  for (i = 0; i < 5; i++)
    switch(i)
    {
      case 1:   printf("*\n");    break;
      case 2:   printf("**\n");   break;
      case 3:   printf("***\n");  break;
      default:  printf("?\n");    break;
    }
  return 0;
}

The solution.

Part C

main()
{
  int x = 10, y = 20; 
  double d = x/y;

  printf("%.2f\n", d);
  d = 7.5;  y = 2;   x = d / y;
  printf("%i\n", x);
  return 0;
}

The solution.

Part D

main()
{
  void tweak(double s[], int t);

  int s = 4;
  double t[4] = {17, 17, 17, 17};

  tweak(t, s);
  printf("%i\n", s);
  printf("%i %i %i %I\n", t[0], t[1], t[2], t[3]);
  return 0;
}

void tweak(double s[], int t)
{
  s[1] = 9;  s[2] = 13;  t = 10;
}

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

/* 
 * Set all 10 elements of table to 100.
 */
main()
{
  int table[10];

  x = 10;
  setTable();
}

void setTable(int v)
{
  for (i = 0; i < x; i++)
    table[i] = v;
}

The solution.

Part B

/* 
 * Set all "n" elements in "t" that match "value" to "newvalue"
 */
int remove(int t[], int n, int value, int newvalue)
{
  int i;
  
  for (i = 1; i < n; i++)
    if (t[i] = value)
      i = newvalue;
}

The solution.

Part C

/* 
 * Read one line of input and return a count of all the commas or periods
 * that appear on the line.
 */
#include 

void countPunct()
{
  int count = 0;
  
  while (c=getchar() != EOF)
    if (c == ',' && c == '.')
      count++;
  return count;
}

The solution.

------

PROBLEM #3 (32 points)

Assume you have a function, writeN, that has already been written for you. writeN takes two arguments, a character, and a number, and writes that character that number of times. For example, writeN('x', 5) writes xxxxx. Using this function, write a new function, displayTable, where the call

tab[0] = 3; tab[1] = -5; tab[2] = 4; tab[3] = 0; tab[4] = -6;
displayTable(tab, 5, 10, '+', '-');
produces this output:
..........!+++.......
.....-----!..........
..........!++++......
..........!..........
....------!..........

A sketch of the function and its expected parameters is shown at the top of the next page. Your job is to fill in its body.

/*
   Display table's parameters:

     a -    A table of integers.  Each array element states how many
            "pos" or "neg" characters to write.  If the value is
            positive, write that number of "pos" characters to the
            right of the !, if it's negative, write that number of
            "neg" characters to the left of the !
     n  -   The number of entries in the table 
     dots - The maximum number of dots to produce before and after the !. 
     pos -  The character to write for a positive array element 
     neg -  The character to write for a negative array element
*/

displayTable(int a[], int n, int dots, char pos, char neg)
{


}
------

The solution.

PROBLEM #4 (20 points)

Write a program that prints only those input characters that appear between double quotes. That is, if the input is:

Some common greetings are "Hi", "Hello", and "Howzit".
the output should be:
Hi
Hello
Howzit

The solution.

------

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