Picture.h
Picture.cpp
g++ -o Photoshop Photoshop.cpp Picture.cpp
g++ -c Photoshop.cpp
g++ -c Picture.cpp
g++ -o Photoshop Photoshop.o Picture.o
./Photoshop < bvz.pgm > edited_bvz.pgm
| (a) | (b) |
Person::Person(string fn, string ln, string addr) {
firstname = fn;
lastname = ln;
address = addr;
}
|
Person::Person(string fn, string ln, string addr):
firstname(fn), lastname(ln), address(addr) {}
|
|---|
The initialization list in constructor (b) requires only one method call to initialize each string while the constructor in (a) requires two method calls to initialize each string. In addition to the assignment statements you see in (a), which require a call to the string assignment operator, the 0-argument constructors for each string variables are also called.
vector<int> v;
int i;
int originalSize;
// (1) resize the vector
originalSize = v.size();
v.resize(v.size() + 3);
// (2) shift the existing entries to the right
for (i = 0; i < v.size(); i++) {
v[i+3] = v[i];
}
// (3) fill the vacated entries with 0's
for (i = 0; i < 3; i++) {
v[i] = 0;
}
Suppose that v contains the contents:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 |
The code fragment as written does not achieve its intended purpose. Answer the following questions:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 10 | 20 | 30 | 10 | 20 | 30 | 10 | 20 |
It is starting to shift the entries from the left side of the vector rather than from the right side of the vector and as a result it is clobbering entries in the vector before it has a chance to move them.
You need to modify fragment 2 so that it starts shifting entries from the right side of the original vector (not the resized vector--that's why my code starts shifting from originalSize-1 rather than v.size()-1):
for (i = originalSize-1; i >= 0; i--) {
v[i+3] = v[i];
}
h(key) = key % 11
| Separate Chaining | Quadratic Probing | |
|---|---|---|
| 0 | ||
| 1 | 12 | 12 |
| 2 | ||
| 3 | 58, 25 | 58 |
| 4 | 48 | 48 |
| 5 | ||
| 6 | 30 | |
| 7 | 25 | |
| 8 | 19, 52, 30 | 19 |
| 9 | 52 | |
| 10 | 32 | 32 |
| key | quadratic probing |
|---|---|
| 32 | 10 |
| 12 | 1 |
| 48 | 4 |
| 19 | 8 |
| 52 | 8, 9 |
| 58 | 3 |
| 30 | 8, 9, 1, 6 |
| 25 | 3, 4, 7 |
^ is bitwise exclusive or:
1001 0101
^ 1010 1100
---------
0011 1001 = 0x39
| is bitwise or
1001 0101
| 1010 1100
---------
1011 1101 = 0xBD
>> is the right shift operator:
1010 1100 >> 3 = 0001 0101 = 0x15
char animal; // the letter of the most recently collected animal
int totalPoints; // the total number of points earned thus far
Write a single C++ statement that will convert the character stored in animal to the correct point value and add that point value to totalPoints
totalPoints += (animal + 1 - 'A') * 2;
string name; double salary; int age;printf("%15s %-12.1f %-6d\n", name.c_str(), salary, age);
#include <iostream>
#include <sstream>
using namespace std;
int main() {
istringstream buffer;
int num;
double num1;
int sum1 = 0;
double sum2 = 0;
string input;
while (cin >> input) {
buffer.clear();
buffer.str(input);
if (buffer >> num1) {
if (input.find('.') == string::npos) {
buffer.clear();
buffer.str(input);
buffer >> num;
sum1 += num;
}
else {
sum2 += num1;
}
}
else {
cout << input << endl;
}
}
cout << "sum1 = " << sum1 << endl;
cout << "sum2 = " << sum2 << endl;
}
|
UNIX> g++ mystery.cpp UNIX> cat input.txt 3.6 14.5 -5 Brad Smiley 20. Jill Nancy 17.4 50 UNIX> ./a.out < input.txt 10 Mario |
Brad
Smiley
Jill
Nancy
sum1 = 45
sum2 = 55.5
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
int main(int argc, char *argv[]) {
istringstream buffer;
vector<string> file;
int numLines;
string line;
int i;
// use a stringstream to convert the command line argument for the number
// of lines to print to an integer
buffer.str(argv[1]);
buffer >> numLines;
// add all the lines in the input to the vector
while (getline(cin, line)) {
file.push_back(line);
}
// If the number of lines in the file is less than the number of lines
// to print then we will print all the lines in the file
if (file.size() < numLines) {
numLines = file.size();
}
// print the last numLines in the file
for (i = file.size() - numLines; i < file.size(); i++) {
cout << file[i] << endl;
}
}
void readTeams(string& group, vector<SVec> &teams) {
string name;
istringstream buffer;
SVec row;
// put the group into an istringstream so we can break it into team members
// and then add the team members to our row. Finally add the new row to
// our teams vector
buffer.str(group);
while (buffer >> name) {
row.push_back(name);
}
teams.push_back(row);
}
void printTeam(string &name, vector<SVec> &teams) {
int i, numNames;
int numMembers;
// do a linear search of teams to find the team leader
for (i = 0; i < teams.size(); i++) {
// if we find the team leader, then print all the team members, 3
// per line. We reach the end of a line whenever the number of names
// we have printed is a multiple of 3. numNames counts the number of names
// printed, but it starts at 0 so we must add 1 to it to get the correct
// number of names we have printed. We then use the mod operator (%) to
// test whether (numNames+1) is a multiple of 3, and if it is, we print
// a newline character so that we start a new line.
if (teams[i][0] == name) {
for (numNames = 0; numNames < teams[i].size(); numNames++) {
printf("%10s", teams[i][numNames].c_str());
if (((numNames+1) % 3) == 0) {
printf("\n");
}
}
// when we finish printing all the team members, we need to print a
// newline character if we did not complete the line. We did not
// complete the line if the number of names printed is not a multiple of 3.
// Please note that we incremented numNames as we exited the loop and therefore
// it now accurately reflects the number of names that we printed.
if ((numNames % 3) != 0)
printf("\n");
printf("\n"); // print an extra blank line
return;
}
}
printf("no team found whose leader is %s\n\n", name.c_str());
}