EE150 - Spring 1996

Solution
To Sample Final

You should resist the temptation to look at these answers until after you have first tried to solve the problems.
Jump To Solution For Problem: [1 | 2 | 3 | 4 | 5]
------

Problem #1

Part A

The first thing you do to trace the code is start by examining what the parameters look like after the example call:

         +------------------------------------------+
x:       | 31 | 17 | 26 | 9 | 6 | 17 | 19 | 17  ... |
         +------------------------------------------+
old: 17
new: 40
stop: 9
In addition, there is a local variable i.

You now execute the loop, which sets i to 0 and then checks whether x[i] (x[0]), which is 31, is greater than stop, which is 9. It is, so it enters the loop, compares x[0] with old, which is 40, finds that they are different, and so sets x[0] to new, which is 40.

The next time through the loop, the loop test compares x[1] with stop and finds that 17 is greater than 9, so it enters the loop, compares x[1] with old, and finds that 17 is equal to 17, and the array doesn't change.

The next time through the loop, x[2] is again greater than stop (26 is greater than 9) so it goes through the loop, and winds up setting this element to 40 (like it did with the first element).

Finally, x[3] is equal to stop, so the loop stops executing, and the function returns i, which is 3.

The problem.

Part B

Since the function returned 3 and 3 is assigned to n, the code writes a 3.

The problem.

Part C

The 1st and 3rd elements were modified, so the table looks like:

         +------------------------------------------+
x:       | 40 | 17 | 40 | 9 | 6 | 17 | 19 | 17  ... |
         +------------------------------------------+

The problem.

Part D

Clearly the loop test is wrong. What we want to do is process the stop elements. That means, i should go from 0 up to stop. So in line 5, the loop test should be i < stop.

Another problem is that we are changing exactly the wrong set of elements. The if test in line 6 is also wrong, where it is comparing x[i] != old, but should instead be checking whether x[i] == old.

Finally, the return value should be a count of changes, not just the index. So we need a count. This involves insert a declaration and initialization of the count before line 4:

int count = 0;
It involves updating the count in the if, which means changing the then part of the if to look like:
{
  count++;
  x[i] = new;
}
And it means changing line 8 to return count instead of i.

The problem.

------

Problem #2

Part A

One solution is:

#include <stdio.h>
#include <ctype.h>

main()
{
  int c;
  int n, i;

  while ((c = getchar()) != EOF)
  {
    if (isdigit(c))
    {
      n = c - '0';
      c = getchar();    /* get character after digit */
    }
    else
      n = 1;
    for (i = 1; i <= n; i++)
      putchar(c);
  }
  return 0;
}

The problem.

------

Problem #3

One solution is the following:

int removeBlanks(char s[])
{
  int count = 0;
  int j = 0;

  for (i = 0; s[i] != '\0'; i++)
    if (s[i] != ' ')   
      s[j++] = s[i];
  s[j] = '\0';
  return j;
}

The problem.

------

Problem #4

Part A

One solution is the following:

int findWord(struct entry table[], int n, char *rapword)
{
  int isSameString(char *str1, char *str2);

  int i;

  for (i = 0; i < n; i++)
    if (isSameString(table[i].word, rapword))
      return i;
  return -1;
}

The problem.

Part B

One solution is the following (using getline from the textbook):

#include <stdio.h>

#define MAXLEN 80
 
main()
{
  int getline(char line[], int n);
  int findWord(struct entry table[], int n, char *word);

  char word[MAXLEN + 1];
  int pos;

  while(getline(word, MAXLEN) != -1)
  {
    if ((pos = findWord(dictionary, MAXWORDS, word)) == -1)
      printf("unknown word!\n");
    else
      printf("%s\n", dictionary[pos]);
  }
  return 0;
}

The problem.

Problem #5

Part A

One solution is:

void makeCounts(int counters[], int n)
{
  int c;
  int index;

  for (index = 0; index < n; index++)    /* zero counters */
    counters[index] = 0;

  while ((c = getchar()) != EOF)            /* read chars and */
    if (c >= 'A' && c <= 'F')         /* update appropriate counter */
    {
      index = c - 'A';
      counters[index]++;
    }
}

The problem.

Part B

One solution is:

void blankOut(char table[MAXROWS][MAXCOLS], int r, int c)
{
  int row, col;

  for (row = 0; row < r; row++)
    for (col = 0; col < c; col++)
      table[row][col] = ' ';
}

The problem.

Part C

One solution is:

void placeStars(char table[MAXROWS][MAXCOLS], int counters[], int n)
{
  int row, col;

  for (col = 0; col < n; col++)
    for (row = 0; row < counters[col]; row++)
      tables[row][col] = '*';
}

The problem.

Part D

One solution is:

void printHisto(char table[MAXROWS][MAXCOLS], int r, int c)
{
  int row, col;

  printf("A B C D E F\n");
  for (row = 0; row < r; row++)
  {
    for (col = 0; col < c; col++)
      printf("%c ", table[row][col]);
    printf("\n");
  }
}

The problem.

Part E

This part is just a set of function prototypes and function calls!

main()
{
  void makeCounts(int counters[], int n);
  void blankOut(char table[MAXROWS][MAXCOLS], int r, int c);
  void placeStars(char table[MAXROWS][MAXCOLS], int counters[], int n);
  void printHisto(char table[MAXROWS][MAXCOLS], int r, int c);

  char output_table[MAXROWS][MAXCOLS];
  int grade_counters[MAXCOLS];

  makeCounts(grade_counters, MAXCOLS);   /* Set up counters */
  blankOut(output_table);                /* Blank out table */
  placeStars(output_table, grade_counters, MAXCOLS);   /* Set up table */
  printHisto(output_table, MAXROWS, MAXCOLS
}

The problem.

------

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