FULL NAME: ____________________________________ 102 or 206 (circle one)
1a. (10 points) You have a two-dimensional array of characters that has NROWS (number of rows)
and NCOLS (number of columns). NROWS and NCOLS are odd global constants.
1b. (10 points) You have an integer array of size N where N is a global constant.
Write function max_min() that finds (and returns through the
parameter list) 1) the largest value in the array, 2) the smallest
value in the array, 3) the number of times the smallest appears, and
4) the number of times the largest appears. Use a single loop.
a. Write function fill() that sets the middle row and middle
column to the character X and the rest of the array elements to
the character O.
b. Write function pretty_print() that displays the array
with NCOLS values per line and one space separating the characters. A
5x7 array would be displayed like this:
O O O X O O O
0 O O X O O O
X X X X X X X
O O O X O O O
O O O X O O O
2. (8 points) You have the following class definition.
class Test2 {
public:
Test2(); // default constructor
Test2(int, int, char*); // parametrized constructor
private:
int x, // range 0 through 99
y; // range 0 through 10
char a[21]; // C-style string, requires srtcpy() to copy
}
a. Write the code for the default constructor where the integer data
members are set to 0, and the string is set to the null string ''.
b. Write the code for the parametrized constructor. Check for valid
integers and appropriate string length. If out of range, set to
the defaults.
c. Create an object obj1 with default values.
d. Create an object obj2 with arguments 5, 8, "test two".
3. (6 points) a. Study the code below and indicate the output.
#include<iostream>
#include<vector>
using namespace std;
void func(int a[], vector<int> v)
{
int i;
v[2] = a[0] + v[1];
a[2] = v[2] - a[1];
v[0] = v[2] + a[1];
cout << "In func(): \n";
for(i = 0; i < 3; i++)
cout << a[i] << " ";
cout << endl;
for(i = 0; i < 3; i++)
cout << v[i] << " ";
cout << endl;
}
int main(void)
{
int a[3] = {10, 11, 12};
vector<int> v(3);
int i;
cout << "Size of vector: " << v.size() << endl;
v[0] = 20; v[1] = 21; v[2] = 22;
func(a, v);
cout << "In main(): \n";
for(i = 0; i < 3; i++)
cout << a[i] << " ";
cout << endl;
for(i = 0; i < 3; i++)
cout << v[i] << " ";
cout << endl;
}
4. (4 points) a. With vectors, precisely what does member size() do? Be complete but brief.
b. With vectors, precisely what does member at() do? Be complete but brief.
5. (7 points) Mark each item as valid or invalid.
If valid, give the value of the left side operand. If invalid, explain why.
Assume that the initializations below are reset for each statement.
int a[6] = {0, 1, 2, 3, 4, 5}, *p = &a[0];
(a) p = a;
(b) a = p;
(c) *p = *(a + 4);
(d) a[3] = *p;
(e) *p = a[2];
(f) p = (a + 4);
(g) p = &a + 4;
struct course {
char title[21];
char day[3], grade;
int hours;
};
struct course schedule[10], extra; // assume initialization
b.  Assign a four to the hours field of the last structure in schedule.
c.  Write one statement to copy the string "ECE 206" to the
title field of the first structure in schedule.
7. (6 points) You have an array-based linked list with
an alphabetic list pointed to by first.
data link first: (see part c)
-----------|------
0 | Lee | -1 | next: 5
-----------|------
1 | Jim | 0 |
-----------|------
2 | Ike | 1 |
-----------|------
3 | Amy | 4 |
-----------|------
4 | Bev | 2 |
-----------|------
5 | | |
-----------|------
b.  Very neatly, show what happens in the arrays when we add Dan.
c.  What signifies an empty list?
8. (6 points) Answer the following questions.
a. What is the primary disadvantage of binary search?
b. What are the two things that new does?
c. What are the two things that delete does?
9. (3 points) You have int a[7] = {24, 37, 12, 64, 19, 45, 71}; Show the array elements after the first pass of selection sort as shown in class.
-------------------------------------------
pass 1: | | | | | | | |
| | | | | | | |
-------------------------------------------
0 1 2 3 4 5 6
10. (8 points) Write function count_ch() that returns the number
of times character ch is in C-style string word. Determine the length
of the string with either strlen() or use the string terminating
"NULL" character, written as '\0', to end your for loop. We have supplied
the header for you.
int count_ch(char word[], char ch)
11. (6 points) Answer the following questions about C-style strings. Recall that C-style strings are character arrays that are terminated by the "NULL" character, which is written '\0'.
char s1[32] = "Almost the end of the semester!";
char s2[5] = "EECS";
char *s3 = "EECS";
b. Is a positive number, a negative number, or zero printed by
        cout << strcmp(s2, s3) << endl; ?
c. What does strchr(s3, 'E') do? Be brief but clear.
13. (6 points) What gets printed from the C++ program below?
#include<iostream>
using namespace std;
void ptr_func(int, int *);
int main()
{
int i, y;
int *p;
p = &i;
i = y = 33;
cout << "Before call to ptr_func(): " << i << " " << *p << endl;
ptr_func(i, p);
cout << "After call to ptr_func(): "
<< i << " " << *p << " " << y << endl;
*p = 55;
cout << "Result: " << i << " " << *p << endl;
return 0;
}
void ptr_func(int x, int *ptr)
{
int y; // y is local to ptr_func()
cout << "During call to ptr_func(): " << x << " " << *ptr << endl;
x = 22;
y = 44;
*ptr = 77;
}
Key for CS 102/ECE 206 Test 2, April 27, 2010
1a.
void fill (char a[][NCOLS])
{
int row, col;
for (row = 0; row < NROWS; row++)
for (col = 0; col < NCOLS; col++)
a[row][col] = 'O';
for (row = 0; row < NROWS; row++)
a[row][NCOLS / 2] = 'X';
for (col = 0; col < NCOLS; col++)
a[NROWS / 2][col] = 'X';
}
void pretty_print (char a[][NCOLS])
{
int row, col;
for (row = 0; row < NROWS; row++)
for (col = 0; col < NCOLS; col++) {
cout << a[row][col] << " ";
if (col == NCOLS - 1) cout << endl;
}
}
1b.
void max_min(int a[], int *max, int *min, int *cmax, int *cmin)
{
int i;
*max = *min = a[0];
*cmax = *cmin = 1;
for (i = 1; i < N; i++) {
if (a[i] == *max) *cmax += 1;
if (a[i] == *min) *cmin += 1;
if (a[i] > *max) {
*max = a[i];
*cmax = 1;
}
if (a[i] < *min) {
*min = a[i];
*cmin = 1;
}
}
}
2a.
Test2::Test2() // default constructor
{
x = y = 0;
strcpy (a, "");
}
b. Mystery::Mystery(int m, int n, char *s) // parameterized constructor
{
cout << m << " " << n << " " << endl << endl;
m >= 0 && m <= 25 ? x = m : x = 0;
n >= 0 && n <= 10 ? x = n : y = 0;
strlen(s) <= 20 ? strcpy(a, s) : strcpy(a, "");
}
Or, use if-else.
c. Test2 obj1;
d. Test2 obj2 (5, 8, "test two");
3. Size of vector: 3
In func():
10 11 20
42 21 31
In main():
10 11 20
20 21 22
4. a. Indicates the actual number of elements in the vector.
b. Checks the index boundaries.
5. (a) valid - address of a[0]
(b) invalid - a is a constant pointer.
(c) valid - 3
(d) valid - 0
(e) valid - 4
(f) valid - address of a[3]
(g) invalid - &a is a double pointer and p is not
6. a. schedule[0] = extra;
b. schedule[9].hours = 4;
c. strcpy(schedule[0].title, "ECE 206");
7. a. Lee Jim Ike Amy Bev
b. -----------|------
0 | Lee | -1 |
-----------|------
1 | Jim | 0 |
-----------|------
2 | Ike | 1 |
-----------|------
3 | Amy | 4 |
-----------|------
4 | Bev | 5 | Note: only link[4] and [5] are changed
-----------|------
5 | Dan | 2 | Dan is added here.
-----------|------
c. first holds -1
8a. The list must be sorted.
b. 1. allocates space from the heap (or freestore) for the variable, and
2. assigns the address of the space to the pointer
c. 1. returns the space to the heap (or freestore), and
2. the pointer is now undefined.
9. 12 37 24 64 19 45 71
10. Lots of correct ways to write this - if you are stuck, come see me.
11. a. negative b. zero
c. returns a pointer to (or the address of) the first instance
of the character 'E' in the string.
12. ifstream fin;
fin.open(in.dat");
13. Before call to ptr_func(): 33 33
During call to ptr_func(): 33 33
After call to ptr_func(): 77 77 33
Result: 55 55