EE150 - Spring 1996

Sample
Final Exam

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

Problem #1 (15 points)

Implement the function rotate. This function is to be passed two arrays, the number of elements in those arrays (assumed to be the same), and a number of elements to rotate. Here's an example:

Array A before function call:        A[0] A[1] A[2] A[3] A[4] A[5] A[6]
                                    +----------------------------------+
                                    | 10 | 13 |  7 | 18 | 20 | 26 | 31 |
                                    +----------------------------------+

Array B before function call:        B[0] B[1] B[2] B[3] B[4] B[5] B[6]
                                    +----------------------------------+
                                    |  ? |  ? |  ? |  ? |  ? |  ? |  ? |
                                    +----------------------------------+

Array B after rotate(A, B, 7, 3):    B[0] B[1] B[2] B[3] B[4] B[5] B[6]
                                    +----------------------------------+
                                    | 20 | 26 | 31 | 10 | 13 |  7 | 18 |
                                    +----------------------------------+
What this call to rotate did is place A's first four values 3 elements later in B (that is, B[3] is A[0], B[4] is A[1], and so on), and it placed A's last 3 values as the first 3 elements of B (that is, B[0] is A[4], B[1] is A[5], and so on). This effectively makes B the equivalent of rotating A 3 items to the right.

Your job is to fill in rotate's body below. HINT: The problem can be solved in 5 lines, which includes a variable declaration.

void rotate(int x[], int y[], int n, int k)
{




}

The solution.

------

Problem #2

Unfortunately, you've written a program that doesn't work. Your program is supposed to read a single integer from the user and draw a triangle with that height. For example, if the user's input is 4, your program's output should be:

   *
  ***
 *****
*******
Below is the program you wrote. It uses a function writeN, which takes a character and a number and writes that character that number of times (you are sure this function works, as you have thoroughly tested it and used it on the last midterm).
#include <stdio.h>

main()
{
  void writeN(char c, int n);
  int i, height;

  scanf("%i", &height);
  for (i = 1; i < height; i++)
  {
    writeN(' ', height - 1);
    writeN('*', i);
    writeN('\n', 1);
  }

Part A (6 points)

What does this program produce as output if the user inputs a 4?

The solution.

Part B (10 points)

Show us how to fix the program. Do this by marking up the program above (showing us where we delete, add, or change lines). HINT: There is more than one change that you need to make! After making any fix, trace through the program to make sure it produces the correct output.

The solution.

------

Problem #3

It's just not your day. You've written another program that doesn't seem to work. The program below is supposed to print the number of 0's, 1's, and other characters in a string.

main()
{
  void countem(char *s, int *p0, int *p1, int *po);
  int zeros = 0, ones = 0, others = 0;

  countem("001x01", &zeros, &ones, &others);
  printf("zeros=%i ones=%i others=%i\n", zeros, ones, others);
  return 0;
}

void countem(char *s, int *p0, int *p1, int *po)
{
  int i;
  int  cnt0 = 0, cnt1 = 0, cnto = 0;

  for (i = 0; s[i] != '\0'; i++)
    if (s[i] == 0)
      cnt0++;
    else if (s[i] == 1)
      cnt1++;
    else
      cnt0++;
}
You expected this program to produce this output:
zeros=3 ones=2 others=1

Part A (6 points)

Show us what this program actually produces as output.

The solution.

Part B (10 points)

Show us how to fix the program. Do this by marking up the program above (showing us where we delete, add, or change lines). HINT: There is more than one change that you need to make! After making any fix, trace through the program to make sure it produces the correct output.

The solution.

------

Problem #4

This problem is to write a program (in stages) that uses two-dimensional arrays to read the input and print it so that the last line is printed first, the first line last, and each line is printed last character first, first character last. Here's an example. For the input:

alex
was
here
the output should be:
ereh
saw
xela
Assume no more than 10 input lines and that each input line contains 20 characters or less.

Part A (2 points)

Draw a picture of the two-dimensional array after the sample input has been read in.

The solution.

Part B (6 points)

Fill in the body of the function fillTwoD, whose job it is to read the input into the two-dimensional array. It returns the number of lines read. Assume the input will fit in the array. HINT: Feel free to use getline or getchar to read the input.

int fillTwoD(char table[][21])
{




}

The solution.

Part C (6 points)

Write the function, printRevLine, whose job it is to output a single line stored in the two-dimensional array in reverse order.

void printRevLine(char table[][21], int lineno)
{

}

The solution.

Part D (5 points)

Using printRevLine, write printRevTwoD, which writes all of the rows in the twoD array in reverse order, starting with the last row and finishing with the first. Its second parameter is the number of input lines read. NOTE: You get ZERO points if you do not use printRevLine, even if your solution works.
void printRevTwoD(char table [][21], int lines)
{






}

The solution.

Part E (4 points)

Write the main program that uses all the functions written in the previous parts to actually solve the problem.

The solution.

------

Problem #5

This problem is to write a variety of functions that manipulate a table of structures. Assume you have the following structure type defined for you:

#define MAX_NAME 50

struct CD
{
  char artist[MAX_NAME + 1];
  char title[MAX_NAME + 1];
  double price;
  int quantityInStock;
};

Part A (10 points)

Write readInput, a function that reads input of the form below into the an array of structures, stopping when it hits EOF. Each entry has three lines, where the first is the CD's artist, the second is the CD's title, and the third contains the CD's price and the number of CD's in stock.
Gin Blossoms
Congratulations, I'm Sorry
12.95 10
TLC
CrazySexyCool
11.95 20
...
The function is passed an empty table and returns the number of entries it successfully read. Assume there are no input errors. Hint: Free free to use getline.
int readTable(struct CD table[])
{


}

The solution.

Part B (10 points)

Write a function, {\tt printArtist}, which prints the titles of all CDs in the table by a particular artist. It is passed the table of CDs, the number of entries in the table, and the artist of interest.
void printArtist(struct CD table[], int n, char artistName[])
{




}

The solution.

Part C (10 points)

Write a function, outOfStock, which creates a new table of all CDs that are out of stock (have a quantityInStock of 0. It is passed the original table, the number of entries in it, and another empty table (which it fills with descriptions of all the CDs that are out of stock). It returns the number of entries in the new table.
int outOfStock(struct CD table[], int n, struct CD newtable[])
{



}

The solution.

------

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