CS140 -- Lab 5


This lab will:

  1. give you some more practice with pointers,
  2. allow you to review a number of C concepts,
  3. give you some practice with algorithm analysis, and
  4. allow you to rework one of your previous labs to regain some points.


Part 1 -- Pointers

The website http://www.problets.org/user/f10/utk/ will lead you through a set of C pointer problems that will take you about 45 minutes to complete. It will give you some additional practice with C-style pointers. Read the instructions first, then perform the exercises. You will need to print out the confirmation page at the end of the exercise and present it to the lab TA to receive credit for this portion of the lab.

Part 2 -- Vflip

For this part you will need to review the background on pgm files that you were given in lab 3. You are going to write a program named vflip that creates a vertical reflection of the input file -- in other words, up is down and down is up. Vflip reads a pgm file given as the first command line argument, and prints to a pgm file given as the second command line argument. The output file will be the vertical reflection of the input file.

You'll have to do a little more now than you did with hflip in lab 3. Allocate an array of row (int *)'s. Then initialize each one of those (int *)'s by calling malloc() to allocate an array of column ints. Read each row of pixels into one of these arrays, and then print the rows out backwards (i.e., in reverse order).

Testing Your Program

You will need to test your program on more than just the pgm files that we have given you. Specifically you will need to test the following error, boundary, and normal conditions:

  1. error conditions: You should given an appropriate error message and exit the program if any of these errors are detected. When appropriate, give the line number in the input file on which the error occurred.
    1. improper number of command line arguments
    2. failure to open the input file
    3. failure to open the output file
    4. invalid input on the first, second, or third line of the pgm file
    5. pixel values that are outside the range 0-255
    6. insufficient input (i.e., there are fewer than row*column pixels in the input). Do not worry about the opposite case in which there are too many pixel values. If there are too many pixel values, simply ignore them.y

  2. boundary conditions: There are no boundary conditions for this program. If the file is empty, then it is an error.

  3. normal conditions
    1. Do not assume that each line of input will have the same number of columns. Remember that a line may have an arbitrary number of pixel values. All that counts is that ultimately the input file have (row * column) pixels.

Things to Know/Do

  1. Copy the pgm files from the ~bvz/cs140/labs/lab3 directory to test your program. Please delete the pgm files when you are done with the lab because they take up a lot of space on the department's file system.

  2. You can run my vflip executable in ~bvz/cs140/labs/lab5 if you have any question as to what your output should look like. It should be run as
    vflip input_pgm_file output_pgm_file
    


Part 3: Analysis of Algorithms

Answer the following questions using an ascii text editor, such as vi, emacs, or notepad, and place your answers in a file named answers.txt.

  1. Programs A and B are analyzed and found to have worst-case running times no greater than 150N log2N and N2, respectively. Answer the following questions (Note: you should not need a calculator to answer any of the following questions):

    1. Which program has the better guarantee on the running time, for large values of N (N > 10,000)?

    2. Which program has the better guarantee on the running time, for small values of N (N < 100)?

    3. Which program will run faster on average for N = 1,000?

    4. If you did not know the size of the input, which program would you prefer to use? Why?

  2. Behold the following function:
         int max_sum(int *array, int size, int *save_i, int *save_j) {
             int max = 0;
    	 int i, j;
    
    	 for (i = 0; i < size-1; i++) {
    	     for (j = i+1; j < size; j++) {
    	          if (array[j] > array[i]) {
    		      if ((array[j] + array[i]) > max) {
    		          max = array[j] + array[i];
    			  *save_i = i;
    			  *save_j = j;
    		      }
    		  }
    	     }
              }
    	  return max;
          }
          
    1. What is T(size) for the inner-most loop? You are allowed to use i in your mathematical expression for T(n). Note that size represents the size of the input for max_sum. Hint: when trying to figure out how many times the inner loop will iterate, choose the most pessimistic value of i possible. Remember that we are looking for the worst case, not the best case or even the average case.

    2. What is T(size) for max_sum as a whole?

    3. What is the big-O running time of max_sum?

    4. Suppose main has the following declarations:
            #define SIZE 10
            int a[SIZE];
            int max;
            int index1;
            int index2;
            

      Write a line of code that will call max_sum and save the return result in max, the value of save_i in index1 and the value of save_j in index2.

  3. Behold the following function:
    int count_words(IS word_file) {
      int sum = 0;
      int i;
      while (get_line(word_file) >= 0) {
        sum += word_file->NF;
      }
      return sum;
    }
          
    In all of the following questions, assume that the word size is bounded by a constant (i.e., the maximum number of characters in a word cannot be greater than some constant, such as 30).
    1. Suppose I tell you that the number of words per line is bounded by a constant (e.g., there is a maximum of 256 words per line). What do you think is an appropriate measure for n?
    2. Suppose I tell you that the number of words per line is unbounded. Now what do you think is an appropriate measure for n?
    3. If I tell you that the number of words per line is bounded, what do you think is the Big-O running time of a single call to get_line? Please do not look at the code for get_line to try to determine your answer. Recall that get_line iterates through a character string array for the line and inserts null characters whereever it finds a blank space. You should be able to get a rough estimate of the running time of get_line from this description.
    4. If I tell you that the number of words per line is unbounded, what do you think is the Big-O running time of a single call to get_line?
    5. What is the Big-O running time of count_words under the assumption that the number of words per line is bounded by a constant. Justify your answer by producing a rough estimate for T(n).
    6. What is the Big-O running time of count_words under the assumption that the number of words per line is unbounded? Justify your answer by producing a rough estimate for T(n). Hint: Don't be surprised if your answer to this question is similar to your answer to the previous question. The important reason is that you understand why it might be the same.

  4. What is Big-O of each of the following functions:
    1. f(n) = 10n + 3n log n + 6
    2. f(n) = 6n3 - 4n2 - 3n - 1000
    3. f(n) = 23
    4. f(n) = .000005n2 + 5000000n - 20


Part 4 (Optional): Rework an Old Lab

Rework one of labs 1-4 and resubmit it. You may get up to half the points back that you lost on the lab. You cannot however, recoup any credit lost for handing in a lab late. If you don't think that it's worth your time to do so, then you may skip this part of the lab.


What to Submit

  1. The confirmation page for part 1. You will need to present this page to one of the lab TAs.
  2. A source file for part 2 named vflip.c
  3. An answer file for part 3 named answers.txt
  4. The source files for the lab you wish to re-submit and a file named README in which you tell the TA which lab you are asking to be re-graded.