- (15 points) Declare a class named PgmImage that you could place in a .h file. Do not write the
method definitions as these would not go in a .h file. PgmImage should have the
following variables and methods:
- picture: a 2D vector of ints
- photographer: a string containing the name of the photographer
who took this picture.
- printPicture: a void method that takes a string parameter named
filename and writes the picture to that file.
- getPhotographer: a method that takes no parameters and
that returns the photographer's name.
- crop: a void method that crops the picture. This method takes
the following parameters:
- croppedImage: a 2D vector of ints. The cropped image will be placed in this
2D vector
- row, col, numRows, numCols: 4 ints specifying the
cropped rectangle. The cropped rectangle starts at (row, col)
in the picture and includes numRows rows and numCols columns.
- (12 points) Suppose I want to declare a class named PgmImage
and use it in a program named ImageManipulator.cpp. I also want to
implement the methods for PgmImage in a separate file from ImageManipulator.cpp.
Answer the following questions:
- If the class declaration for PgmImage is placed in a file
named PgmImage.h, then what statement would you write
to include this header file in .cpp files?
- In what file should the method definitions for PgmImage be placed?
Give the name of the file, following the naming conventions discussed
in class.
- Using a single g++ statement, how could I compile all of these files into an
executable named ImageManipulator?
- Suppose instead that I wanted to separately compile these files and
then link together their object files into an executable named
ImageManipulator. Write the set of statements that would be required
to perform this separate compilation and then linking into the
executable.
- Suppose that I execute ImageManipulator
from the command
line by writing the following command:
./ImageManipulator < bvz.pgm
What does the < operator cause the C++ program to do?
- (8 points) In a function, why should you use reference parameters for objects
(e.g., strings, vectors, instances of classes),
rather than value parameters, even if you do not intend to modify the object? For example, if you want to pass a vector
object to a function, you should declare the vector parameter as follows:
void mystery(string &vector) { ... }
Use no more than 3 sentences for your answer.
- (10 points) Answer the following two questions about typedefs:
- Write a typedef statement that declares a new type named Pixels to be a vector of ints.
- Declare picture to be a vector of Pixels.
- (24 points)
- Insert the keys 22, 28, 39, 48, 41, and 61 into
the following two hash tables using a) separate chaining, and b) quadratic
probing. For separate chaining, use commas to separate the keys that
would go into an entry. For example, if the keys 22, 28, and 39 all
went into the same entry, you would write (22, 28, 39) in that entry.
You should use the hash function:
h(key) = key % 13
| Separate Chaining | Quadratic Probing |
0 | | |
1 | | |
2 | | |
3 | | |
4 | | |
5 | | |
6 | | |
7 | | |
8 | | |
9 | | |
10 | | |
11 | | |
12 | | |
-
For quadratic probing
please also complete the following table showing the comma, separated list of entries you
would probe in order to insert each key (e.g., if you examined entries 6, 7, and 11 in order to insert 48, then you would write (6, 7, 11) next to 48
in the below table--this is only an example--it is not the actual set
of entries you will probe for 48). This will
allow us to assign you partial credit if you do not get the entries
correct in the quadratic probing hash table.
key | quadratic probing |
22 | |
28 | |
39 | |
48 | |
41 | |
61 | |
- (12 points) The intent of the following
code fragment is to read a series of name/salary
pairs from stdin and to print the average salary to stdout.
1 string name;
2 int salary;
3 int salarySum = 0;
4 while (!cin.eof()) {
5 cin >> name >> salary;
6 salarySum += salary;
7 }
8 cout << "total salary = " << salarySum << endl;
- Show what the code fragment prints if the input is:
Suzy 100
Frank 80
Ebber 50
- What is wrong with the code that causes the output to be incorrect?
You may use no more than four sentences.
A sample explanation might be:
The code fragment is incorrect because it is missing
a read statement before the beginning of the while loop and hence the loop
variable is uninitialized when the while statement is reached. This could
cause the while loop to never execute. (this is an example
explanation that does not correspond to the code fragments below!).
- Modify the incorrect portion of the code to make it correct. Please do
not re-gurgitate the entire code. Only modify the incorrect portion.
- (8 points) The following code fragment is supposed to read names into a vector.
1 vector<string> names;
2 int i = 0;
3 string name;
4 while (cin >> name) {
5 names[i] = name;
6 i++;
7 }
- What is wrong with the code? (you may use no more than 4 sentences)
- Change the incorrect portion of the code to make it correct. Do not
regurgitate the entire code. Only modify the incorrect code fragment.
- (12 points) Please read the following instructions before attempting this problem:
- For part (a) convert the hexadecimal
number to binary (0's and 1's).
- For parts (b)-(d) give the hexadecimal result of the operation. Assume that there is only 8 bits in which to store the result (this is only relevant for part d). For these three parts, if you cannot figure out the hexadecimal representation of the answer you can get 2 points for each part by giving the binary representation of the answer.
- You can show your work in the blank spaces to
get partial credit:
- 0xB5 = ____________________________
- (0101 1101) ^ (1110 1100) = _____________________________
- (0010 0101) | (1010 1100) = _____________________________
- (1010 1100) << 3 = _______________________________
- (5 points) ___________________ What is the output of the following printf statement:
char value = 'a' + 6;
printf("%c", value);
- (14 points) Suppose you are given the following variable declarations:
string runner;
int minutes, seconds;
Write a printf statement that:
- outputs runner in a left-justified field of 15 characters, and time
in the following two formats: a) as a right-justified floating point number in a
field that is minimally 6 characters wide with 1 decimal digit where the whole number is the minutes and the fractional number is the seconds divided by 60, and b) as a right-justified string in the format mm:ss (i.e., both minutes and seconds should be in fields that are 2 characters wide).
- If the number of seconds is less than 10, pads the seconds output with
a single 0, and
- Puts a single space between each of the three fields (but no space at
the end of the output).
For example, if the runner's name is "Bradley", minutes is
5, and seconds is 40, then the output would be:
Bradley 5.7 5:40
(40/60 = .67 which when rounded to one digit is .7) and if seconds were instead 4, then the output would be:
Bradley 5.1 5:04
- (Extra Credit-12 points) You have written a program that reads the exam scores for a student
and for each student prints the student's name and exam average.
Your program reads lines of input where the first
and second words on the line are a student's first and last name and the
remaining portion of the line are scores for the student. For example:
Donald Duck 20 40 50
All exam scores must be integers between 0 and 100 and every student
must have at least one exam score. A line of input must therefore have
at least 3 inputs--a firstname, lastname, and test score.
When you test your program, you need to come up with a series of test
cases for correct input, incorrect input, and boundary cases. List
all the test cases you should provide in order to thoroughly test your
program (this is what you are supposed to do in each of your labs). I
will get you started with a couple of example test cases:
Non-erroneous input: This will include all "normal" data plus
all boundary cases:
- Multiple lines of data that have a first name, last name,
and several exam scores between 0 and 100.
- A single line of data that has a first name, last name, and
several exam scores between 0 and 100 (this is called a boundary
case because it contains the minimum possible number of lines of
input)
Hint: The remaining test cases all have to do with boundary
cases involving test scores. I found 3 such cases but feel free to
list more if you think of more cases.
Erroneous input: A test case with:
- A line that has fewer than 3 inputs and is therefore
missing either a name or an exam score.
Hint: The remaining test cases all have to do with erroneous
exam scores as it is not really possible to test for erroneous
names. I found three such cases but feel free to list more
if you think of more cases.