linker | compiler | ls | cd | gdb |
g++ | vector | array | struct | class |
private | protected | public | substr | find |
Ballot.h
Ballot.cpp
g++ -o Vote Vote.cpp Ballot.cpp
g++ -c Vote.cpp g++ -c Ballot.cpp g++ -o Vote Vote.o Ballot.o
./Vote < ballots.txt > results.txt
h(key) = key % 13
Separate Chaining | Quadratic Probing | |
---|---|---|
0 | 52 | 52 |
1 | ||
2 | 15 | 58 |
3 | 15 | |
4 | ||
5 | ||
6 | 32, 19, 58 | 32 |
7 | 19 | |
8 | ||
9 | ||
10 | 49 | 49 |
11 | ||
12 |
key | quadratic probing |
---|---|
32 | 6 |
49 | 10 |
19 | 6, 7 |
52 | 0 |
58 | 6, 7, 10, 2 |
15 | 2, 3 |
string name = "VJ"; unsigned char h = 41; int i; for (i = 0; i < name.size(); i++) { h = (h << 2) ^ name[i]; } |
where 'V' = 0x56 and 'J' = 0x4a What is the hexadecimal value of h after each iteration of the above loop? For this problem assume that unsigned ints are 8 bits
iteration | h |
---|---|
0 | f2 |
1 | 82 |
41 = (1 * 32 + 0 * 16) + (1 * 8 + 0 * 4 + 0 * 2 + 1 * 1) = (1 * 25 + 0 * 24) + (1 * 23 + 0 * 22 + 0 * 21 + 1 * 20)
h = (h << 2) ^ 0101 0110 # I am separating the bits into groups of 4 for clarity
1010 0100 (h << 2) ^ 0101 0110 (0x56) --------- 1111 0010 = 0xf2 (the answer to iteration 0)
h = (h << 2) ^ 0100 1010
1100 1000 (h << 2) ^ 0100 1010 (4a) --------- 1000 0010 = 0x82 (the answer to iteration 1)
If you cannot figure out the hexadecimal value, you can give the base-10 value for a 2 point deduction.
0xf2 = 242 0x82 = 130
(a) | (b) |
void insert(int num, vector<int> &data) { int i; data.resize(data.size()+1); for (i = 0; i < data.size()-1; i++) data[i+1] = data[i]; data[0] = num; } |
void insert(int num, vector<int> &data) { int i; data.resize(data.size()+1); for (i = data.size()-2; i >= 0; i--) data[i+1] = data[i]; data[0] = num; } |
---|
The code right-shifts each element of the vector by 1 position starting from the rear of the vector. By starting at the rear of the vector each element moves into a previously unoccupied "bucket" and does not clobber any other element of the vector. When the right shifting is complete, the first bucket in the vector is empty, and can be filled with the new number.
The code tries to right-shift each element of the vector by 1 position, but it starts at the front of the vector. By starting at the front of the vector, each element moves into a previously occupied "bucket" and clobbers the element to its right. When the right shifting is done, every element of the vector is equal to the former first element of the vector and the original contents of the vector have been lost.
(a) | (b) |
vector<string> readStrings() { string name; vector<string> names; while (cin >> name) { names.push_back(name); } return names; } |
void readStrings(vector<string> &names) { string name; while (cin >> name) { names.push_back(name); } } |
---|
It passes the vector to be filled by reference, rather than by value. Hence instead of copying the vector into the local parameter, the vector in main is used instead.
It returns a vector from the function which requires that the returned vector be copied back to the main function. This copying is inefficient if the vector is large.
typedef vector<string> Marbles
hh:mm:ssEach of the three fields should be 2 spaces wide. The minutes and seconds should be padded with a 0 if they are single digits, but hours should not be. For example "3:06:05".
int hours, minutes, seconds;
printf("%2d:%02d:%02d", hours, minutes, seconds);
letter = 'A' + letter - 'a';