#include <iostream>
using namespace std;
int main(int argc, char **argv) {
int num1, num2;
cin >> num1 >> num2;
cout << num1 + num2 << endl;
return 0;
}
When the user tries to execute the program with the command:
./sum 3 6nothing happens--nothing gets printed and you do not get returned to the unix prompt. It appears as though the program is in an infinite loop, but it is not. Why is the program unresponsive and what must you do to get the program to print something and terminate? Your answer must be no longer than 4 sentences.
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 < originalSize; 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 is intended to produce the following result:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
h(key) = key % 11
| Linear Probing | Quadratic Probing | |
|---|---|---|
| 0 | ||
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 | ||
| 7 | ||
| 8 | ||
| 9 | ||
| 10 |
|
|
int a, b;
int *x, *y;
Also suppose that the above variables are assigned the following memory addresses:
a: 0x1000
b: 0x1004
x: 0x1008
y: 0x100c
After the following code sequence executes, what are the values of a, b, x,
and y?
x = &b;
y = x;
a = 10;
b = 40;
*x = 30;
*y = *x * 3;
a:
b:
x:
y:
#include <iostream>
#include <sstream>
using namespace std;
int mystery() {
double num;
int sum = 0;
istringstream buffer;
string input;
while (cin >> input) {
try {
buffer.clear();
buffer.str(input);
if (buffer >> num) {
if (input.find('.') != string::npos) {
throw num;
}
else {
sum = sum + (int)num;
}
}
else {
cout << "Oops-that was not a number!\n";
throw (string) "input was not a number";
}
cout << "input = " << num << endl;
cout << "intermediate sum = " << sum << endl;
}
catch (double n) {
cout << input << " is a double\n";
}
}
cout << "leaving sum\n";
return sum;
}
int main() {
int result;
try {
result = mystery();
}
catch (const string &e) {
cout << e << endl;
cout << "exiting program with an error\n";
return 1;
}
cout << "sum = " << result << endl;
return 0;
}
|
UNIX> g++ mystery.cpp UNIX> cat input.txt 3.6 16 14.5 -5 Brad Smiley 20. Jill Nancy 17.4 50 UNIX> ./a.out < input.txt |