// CS116 - Summer 1998 // HW2 Problem 1 Solution #include bool even(int num) { return((num % 2) == 0); } // display checker board with x & o void displayCheckerBoard(int numRows, int numCols) { int currentRow, currentCol; // put an 'x' if (row + col) is even otherwise put 'o' for (currentRow = 0; currentRow < numRows; currentRow++) { for (currentCol = 0; currentCol < numCols; currentCol++) { if (even(currentRow + currentCol)) cout << 'x'; else cout << 'o'; } cout << endl; } } main () { int numRows, numCols; // prompt user for number of rows and columns cout << "Please enter the number of rows: "; cin >> numRows; cout << "Please enter the number of columns: "; cin >> numCols; // draw checker board of given dimensions displayCheckerBoard(numRows, numCols); } // CS116 - Summer 1998 // HW2 Problem 2 Solution #include enum BulbState { On, Off }; main() { // an array of 150 light bulbs const int ArraySize = 150; BulbState bulbArray[ArraySize]; int index; // initialize array to Off for (index = 0; index < ArraySize; index++) { bulbArray[index] = Off; } // do switching rounds int round; for (round = 1; round <= ArraySize; round++) { for (index = round - 1; index < ArraySize; index = index + round) { bulbArray[index] = (bulbArray[index] == Off) ? On : Off; } } // display all On bulbs cout << "Light bulbs that are on at the end: "; for (index = 0; index < ArraySize; index++) { if (bulbArray[index] == On) cout << (index + 1) << " "; } cout << endl; }