NAME: _____________________________________ GRADE (TA use only) _____________
if(x != 20 && y)
x++;
else if(x < 0 || y == 20)
y--;
else x += y;
cout << x << " " << y << endl;
a. Rewrite the following C++ code segment to make it more efficient and to remove any unnecessary code.
if(x < y && y >= x) cout << "Okay.\n";
b. Rewrite the following C++ code segment to make it more efficient and
to remove any unnecessary code.
if(x < y && y > x) cout << "Okay.\n";
c. Rewrite the following C++ code segment to make it more efficient and
to remove any unnecessary code.
if(x < y || y >= x) cout << "Okay.\n";
x = 5; y = 5;
if (y <= x) {
if (x > 8)
x++;
}
else
y--;
cout << x << " " << y << endl; cout << " Please enter an integer ";
cin >> x;
switch(x) {
case 30: x--; x--; x--; break;
case 5: x = x * x;
case 27: x--; break;
case 25: x %= 6;
case 20: x += 35;
default: x++;
}
cout << x << endl;
for(i = 0; i < 3; i++) {
cin >> x;
cout << x % 5 << " ";
}
cout << endl << i << " " << x << endl;
#include<iostream>
using namespace std;
void mystery(int&, int);
int main()
{
int x, y, z;
x = 22; y = 44; z = 66;
mystery(x, y);
cout << "In main: " << x << " " << y << " " << z << endl;
}
void mystery(int& x, int b) {
int z;
x = 33; b = 55; z = 77;
cout << "In mystery: " << x << " " << b << " " << z << endl;
}
int mystery(int x)
{
return x / 100;
}
Function mystery( ) returns
b. Write a prototype for function mystery( ).
for(i = 2, x = 1; i <= 10 && x <= 20; i++)
x = x * i;
cout << x << " " << i << endl;
char array[NROWS][NCOLS]; // NROWS and NCOLS are global constants
Write a code segment to put a dollar sign in the upper right and lower left
corners of array.
const int SIZE = 10;
int array[SIZE] = {0}, index;
for(index = 1; index < SIZE; index += 2) // start at index 1
array[index] = 5;
Fill in the values for the array elements.
0 1 2 3 4 5 6 7 8 9
int w = 256, x = 43, y = 10, z;
double c;
a. z = x/y + y/x; z:
b. z = x % y; z:
c. z = w % y * y; z:
d. z = w % 100 / y; z:
e. c = x/y; (Careful!) c:
a. i = j = x = y = 0; // set variables to 0 for(i = 15; i > 10; i--) { x++; for(j = 44; j < 44; j++) y++; } cout << i << " " << j << " " << x << " " << y << endl; b. i = j = x = y = 0; // set variables to 0 for(i = 10; i < 10; i++) { x++; for(j = 0; j > 33; i--) y++; } cout << i << " " << j << " " << x << " " << y << endl;
ifstream infile;
infile.open ("mydata.dat");
if (infile.fail())
{
cout << "Input file failed to open.\n");
exit(1);
}
a. What actual file holds the data originally?
b. Is infile an object or a class?
c. What effect does exit(1) have on the program?
d. True or false, the open statement initializes the
stream variable infile by connecting it to mydata.
BONUS: In recognition of the outstanding effort of the
Tennessee Lady Vols basketball team, this test has 25 free points rather
than 20. Congratulations on winning the SEC!!!
1. a. -15 -1
b. 80 60
c. 31 50
2. a. if(x <= y) cout << "Okay.\n"; -OR- if(y >= x) cout << "Okay.\n";
b. if(x < y) cout << "Okay.\n"; -OR- if(y > x) cout << "Okay.\n";
c. if(x <= y) cout << "Okay.\n"; -OR- if(y >= x) cout << "Okay.\n";
3. 5 5
4. 1. 27
2. 24
3. 56
5. 3 0 4
3 14
6. In mystery: 33 55 77
In main: 33 44 66
7. a. Function mystery() truncates the two rightmost digits of a.
b. int mystery(int); -OR- int mystery(int parm_name);
8. a. 24 5
b. i = 2;
x = 1;
while(i <= 10 && x <= 20)
{
x = x * i;
i++;
}
9. array[0][NCOLS-1] = '$';
array[NROWS-1][0] = '$';
10. 0 5 0 5 0 5 0 5 0 5
11. a. 4
b. 3
c. 60
d. 5
e. 4.00
12. if(x >= -10 && x <= 10)
13. a. 10 44 5 0
b. 10 0 0 0
14. a. mydata.dat
b. object
c. The program terminates.
d. true