CS140 -- Lab 3
- CS140 -- Data Structures
- James S. Plank
-
http://www.cs.utk.edu/~plank/plank/classes/cs140/Labs/Lab3
- Lab Directory: /home/plank/cs140/Labs/Lab3
- The following functions are due at the intermediate deadline:
- pgm_write
- pgm_create
- pgm_pad
- pgm_panel
These four functions are tested by test cases 1-10 and 31-76 (56 test
cases total).
- The lab grade will be determined as follows:
- partial submission-correctness: 30% (hard deadline: no late submission)
- final submission-correctness: 50%
- comments and style: 20%
The correctness grade for your final submission will test
all of the test cases, including the ones for the partial submission.
So if you do not get some of the test cases correct for the partial
submission, try to correct them for the final submission.
- If you are a Windows user, you might find that Windows appends a
new line to the end of your pgm_editor.cpp file. This could cause
Dr. Plank's gradescripts to think that you've changed the file. You
can correct the problem by going to the end of the pgm_editor.cpp
and deleting the newline. Only do this after you've copied the
pgm_editor.cpp file back to the lab machines.
- You may not use the vector.erase() function for this lab. We
want you to implement that functionality yourself.
Back to PGM files.
To do this lab, you are
to copy the program
pgm_editor.cpp
from /home/plank/cs140/Labs/Lab3. You'll note that there is a comment that
says "DO NOT CHANGE ANYTHING BELOW THIS LINE". I feel that this comment is pretty
self-explanatory. The gradescript will check to see if you have changed anything
below the line, and if you do, it will flag it as an error. What you do is write
code where it says "Write your code here". You compile this program and run it,
and that is what you submit for grading.
Here is pgm_editor.cpp:
#include <cstdio>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
typedef vector <int> IVec;
// Write your code here:
// DO NOT CHANGE ANYTHING BELOW THIS LINE
void bad_pgm(string s)
{
cerr << "Bad PGM file: " << s << endl;
exit(1);
}
....
|
Your job is to write seven procedures. You can figure out their parameter
types from how they are called within pgm_editor.cpp. They all
revolve around PGM files, which are held in a vector of vectors of ints.
Suppose that we have a PGM file p, declared as:
Then p.size() is the number of rows of the PGM file. Each element p[i]
will be a vector of the same size. p[i].size() is the number of columns in the
PGM file. And p[i][j] contains the pixel value in row i column j.
In the procedures that you write, you may assume that you get a valid PGM file as
input. You do not have to error check.
The seven procedures are:
- pgm_write(p): This writes a PGM file p to standard output. I want the
output to be in a very specific format:
- The first line is "P2"
- The second line is the number of columns followed by a space and the number of rows.
There should be no special formatting to the second line.
- The third line is the word "255".
- The remaining lines have the pixels, 20 per line, each pixel printed with
printf("%4d") and no extra spaces. Obviously, the last line may be incomplete.
- pgm_create(r, c, pv). This creates and returns a PGM file which has r
rows, c columns, and all pixels have the value pv. You actually
return a 2D vector, not a file from this function.
- pgm_cw(p). This rotates p 90 degrees clockwise. Obviously, p
must be a reference parameter.
You will need to declare a 2D vector to
hold the rotated pgm and then copy the rotated pgm back into the original
pgm vector.
- pgm_ccw(p). This rotates p 90 degrees counterclockwise. Obviously, p
must be a reference parameter.
You will need to declare a 2D vector to
hold the rotated pgm and then copy the rotated pgm back into the original
pgm vector.
- pgm_pad(p, w, pv). This adds w pixels around the border of p. All the pixels
will have the value pv.
You may not declare another 2D vector
to hold the padded pgm file. You can accomplish the padding using only
the original pgm vector.
- pgm_panel(p, r, c). This changes p so that it has r*c copies of
the PGM file, laid out in a r * c grid. See the examples.
You may not declare another 2D vector
to hold the padded pgm file. You can accomplish the paneling using only
the original pgm vector.
- pgm_crop(p, r, c, rows, cols). This changes p so that it has a subset of
the original picture -- the rectangle starting at row r and column c, with
rows rows and cols cols.
You may not declare another 2D vector
to hold the padded pgm file. You can accomplish the cropping using only
the original pgm vector.
When pgm_editor is compiled and running correctly, you can use it either to create
a PGM file, or to edit the PGM file on standard input and print the result on standard
output. For example:
UNIX> pgm_editor
usage: pgm_editor command....
CREATE rows cols pixvalue
CW
CCW
PAD pixels pixvalue
PANEL r c
CROP r c rows cols
UNIX>
UNIX> pgm_editor CREATE 50 200 0 | convert - example_create.jpg

UNIX> pgm_editor CW < Red.pgm | convert - example_cw.jpg

UNIX> pgm_editor CCW < Red.pgm | convert - example_ccw.jpg

UNIX> pgm_editor PAD 30 0 < Rodney.pgm | convert - example_pad_1.jpg

UNIX> pgm_editor PAD 30 0 < Rodney.pgm | pgm_editor PAD 30 255 | pgm_editor PAD 1 0 | convert - example_pad_2.jpg

UNIX> pgm_editor PANEL 2 4 < Red.pgm | convert - example_panel_1.jpg

UNIX> pgm_editor PAD 6 0 < Rodney.pgm | pgm_editor PAD 3 255 | pgm_editor PANEL 3 5 | convert - example_panel_2.jpg

UNIX> pgm_editor CROP 45 60 50 100 < Red.pgm | convert - example_crop.jpg

UNIX> pgm_editor CROP 45 60 50 100 < Red.pgm | pgm_editor PAD 2 0 | pgm_editor PAD 1 255 | pgm_editor PANEL 4 9 | convert - example_crop_panel.jpg

BVZ Notes
- Dr. Plank's main is not declared to return an int, which could cause
your program not to compile on a Mac. You can fix this problem by having
your main return an int when compiling and testing it on a Mac. However,
when you transfer the file to a hydra machine to test it, you will need
to change main back to not returning an int. If you do not, then Dr. Plank's
gradeall script will flag your program as incorrect, since you will have
changed something below the "Do Not Change Anything" line.
- In pgm_write, you need to ensure that your pgm file ends with exactly
one newline character. Hence if the number of pixels in your pgm file is
not a multiple of 20, then you will need to output a newline character
at the end of your pgm_write procedure. If the number of pixels in your
pgm file is a multiple of 20, then your pgm_write procedure will already
have written a newline character and you should not print a second one.
Grading and Starting
Once again, you cannot modify the part of the program
that says "DO NOT CHANGE ANYTHING BELOW THIS LINE.". If so, the gradescript will
yell at you and won't get it correct.
To start this lab -- and do this during lab, write dummy versions of these
procedures that don't work, but that compile. That way you at least have the procedure
declarations correct. Then work on pgm_create() and pgm_write().
Then the others. Think about when you should be using reference parameters.