GRADE (TA use only) __________________
(1 point) USER NAME: ___________________________
#include <iostream>
using namespace std;
int main()
{
int num;
while (1) {
cin >> num;
if (cin.fail()) {
cout << endl;
return 0;
}
cout << "number: " << num << endl;
}
}
#include <iostream>
using namespace std;
int main()
{
int num;
string dummy;
while (1) {
cin >> num;
if (!cin.fail()) {
cout << "Number: " << num << endl;
} else if (cin.eof()) {
return 0;
} else {
cin.clear();
cin >> dummy;
}
}
}
char two_d [5][7];
int row, col;
for(row = 0; row < 5; row++)
for(col = 0; col < 7; col++)
if(col % 2)
two_d[row][col] = 'X';
else
two_d[row][col] = 'O';
0 1 2 3 4 5 6
-----------------------------
0 | | | | | | | |
-----------------------------
1 | | | | | | | |
-----------------------------
2 | | | | | | | |
-----------------------------
3 | | | | | | | |
-----------------------------
4 | | | | | | | |
-----------------------------
How many probes will it take to find   11?
A. Write a function initialize_vector() to read values into vector vec.
-OR-
B. The string function strcmp() is used to compare two strings. Write a complete function my_compare() that is almost functionally equivalent to strcmp(). Your function should return -1, 0, or 1. You may not use any string library functions.
*** Bonus (2 points, all or none, no partial credit)
Examine the function below. What (NOT HOW) does mystery() do?
void mystery(char *one, char *two)
{
char *p, *q;
for(p = one, q = two; *q != '\0'; q++, p++)
*p = *q;
*p = '\0';
}
- Circle::Circle(double r) { radius = r; } Circle::~Circle() { cout << "It's gone!\n"; }
- number: 25 number: -3
- 1. To read past an input value that cannot be converted to an integer. 2. Number: 25 Number: -3 Number: 44 Number: 87
- To prevent multiple definition errors.
- After one() 88 22 After two() 88 22
- 1. 1) A copy of the argument is made; 2) any action by the function is performed on the copy, not the original argument. 2. 1) A copy of the pointer (an address) is passed; 2) the value pointed to by the original argument can be modified. 3. 1) An alias is created for the variable; 2) any action by the function is performed on the original argument.
- int maximum(int x, int y, int z) { int max; // ultimately, holds max value max = x; // assume the largest is x if(y > max) max = y; if(z > max) max = z; return max; }
- 15 32 40
- Yes No!
0 1 2 3 4 5 6 ----------------------------- 0 | O | X | O | X | O | X | O | ----------------------------- 1 | O | X | O | X | O | X | O | ----------------------------- 2 | O | X | O | X | O | X | O | ----------------------------- 3 | O | X | O | X | O | X | O | ----------------------------- 4 | O | X | O | X | O | X | O | ------------------------------ In parm: 33 77 76 43 In main: 76 57 76 57
- The winner is Tennessee in position 1.
- ------------------------------------------- pass 1: | 11 | 55 | 77 | 44 | 66 | 22 | 33 | ------------------------------------------- 0 1 2 3 4 5 6 ------------------------------------------- pass 2: | 11 | 22 | 77 | 44 | 66 | 55 | 33 | ------------------------------------------- 0 1 2 3 4 5 6
- 2 4
- A. vector <int> initial_vector(vector <int> vec) { int i; for(i = 0; i < vec.size(); i++) cin >> vec[i]; return vec; } B. int compare(char *a, char *b) // or int compare(char a[], char b[]) { int i; /* Loop until two characters differ or until end-of-string */ for(i = 0; a[i] == b[i] && (a[i] !='\0' && b[i] != '\0'); i++); /* Last chars examined are different or the same, including '\0' */ if(a[i] < b[i]) return -1; /* string a < string b */ else if(a[i] == b[i]) return 0; /* string a = string b */ else if(a[i] > b[i]) return 1; /* string a > string b */ } *** Bonus
It copies the second string into the first one. That is, it is functionally equivalent to strcpy().