CS211 Quiz 2 Name ______

Questions #1-4 (0.75 points per question) Below is a program that solves the 8 queens problem using a 2D array and backtracking. Fill in the blanks so that the program will work correctly.

#include using namespace std; int main() { int q[8][8] = {1}, r, c = 0, solution = 0; nextCol: ++c; if (______) // Question #1 goto print r = -1; nextRow: ++r; if (______) // Question #2 goto backtrack;

for (int i = 0; i < c; ++i) // row test if (q[r][i] == 1) goto nextRow; for (int i = 1; r - i >= 0 && c - i >= 0; ++i) // up-diagonal test if (q[r-i][c-i] == 1) goto nextRow; for (int i = 1; r + i < 8 && c - i >= 0; ++i) // down-diagonal test if (q[r+i][c-i] == 1) goto nextRow; q[r][c] = 1; goto ______// Question #3 backtrack: --c; if (c < 0) ______// Question #4 for (r = 0; q[r][c] != 1; ++r); q[r][c] = 0; goto nextRow; print: cout << "Solution #" << ++solution << ":\n"; for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) cout << q[i][j]; cout << "\n"; } goto backtrack; CS211 Quiz 2 Name ______

} Question #5 (1 point) Imagine you are running the program on the previous page, which uses a 2D array and backtracking. A queen has just been removed from q[7][3] (the last square of the 4th column, circled in the diagram). On which square will a queen be placed next? Remember that a queen is only placed on a square if all 3 tests pass. Draw an X on the correct square.

Question #6a (1 point) Below is an incomplete ok function for the 8 queens 1D program. It does not perform all 3 of the tests. Which test(s) does it perform? Circle the correct answer.

A. Row test B. One of the diagonal tests C. Both diagonal tests bool ok(int q[], int c) { for (int i = 0; i < c; ++i) if (q[c] - q[i] == c - i) return false; return true; }

Question #6b (0.75 point extra credit) In how many ways can 4 rooks be placed on an 4x8 chessboard (4 rows, 8 columns) such that no two rooks attack each other? No two rooks may be in the same row or column (but they may be in the same diagonal). Either tell me the exact number, or tell me what you would type into your calculator to get the answer.

Question #7 (2 points) Complete the adjacency matrix for the cross below, using -1 as a sentinel value. The first four neighbor lists have been done for you. The numbers in the cross are the indices (aka labels). ______static int adj[8][5] = { |0 |1 | {-1}, // 0 _____|_____|_____|_____ {0, -1}, // 1 |2 |3 |4 |5 | {0, -1}, // 2 |_____|_____|_____|_____| {0, 1, 2, -1}, // 3 |6 |7 | CS211 Quiz 2 Name ______

|_____|_____|

Questions #8-11 (0.5 points per question) Question #12 (0.75 point extra credit) Write the output of each cout statement. For addresses, you may write your answer in the form "address of b + [number] bytes". If you write the address in hexadecimal, you will receive 0.1 extra credit. Assume that the size of an int is 4 bytes, and the size of a pointer is 8 bytes.

#include using namespace std; int main() { int b[3][5] = {{3, 5, 7, 9, 11}, {13, 15, 17, 19, 21}, {23, 25, 27, 29, 31}};

cout << sizeof(*(b+1)) << endl; ______// Q8

cout << sizeof(b+1) << endl; ______// Q9

cout << b << endl; // this line prints 0x22fe10

cout << b + 1 << endl; ______// Q10

cout << &b + 1 << endl; ______// Q11

cout << *(*(b + 2) - 3); ______// Q12

return 0; }