CS140 -- Lab 5
This lab will:
- give you some more practice with gdb,
- allow you to review a number of C concepts,
- give you some practice with algorithm analysis, and
- allow you to rework one of your previous labs to regain some points.
Part 1 -- GDB
Read through the gdb tutorial and do what you are
told to do.
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:
- 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.
- improper number of command line arguments
- failure to open the input file
- failure to open the output file
- invalid input on the first, second, or third line of the pgm file
- pixel values that are outside the range 0-255
- 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
- boundary conditions: There are no boundary conditions for this program.
If the file is empty, then it is an error.
- normal conditions
- 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
- 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.
- 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.
-
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):
- Which program has the better guarantee on the running time, for
large values of N (N > 10,000)?
- Which program has the better guarantee on the running time, for small
values of N (N < 100)?
- Which program will run faster on average for N = 1,000?
- If you did not know the size of the input, which program would
you prefer to use? Why?
- 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;
}
- 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.
- What is T(size) for max_sum as a whole?
- What is the big-O running time of max_sum?
- 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.
- 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).
- 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?
- Suppose I tell you that the number of words per line is unbounded.
Now what do you think is an appropriate measure for n?
- 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.
- 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?
- What is the 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).
- What is the 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.
- What is Big-O of each of the following functions:
- f(n) = 10n + 3n log n + 6
- f(n) = 6n3 - 4n2 - 3n - 1000
- f(n) = 23
- 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. 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
- A source file for part 2 named vflip.c
- An answer file for part 3 named answers.txt
- 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.
There is nothing to submit for part 1.