Embercode
✕
C++ PatternsQuick quiz

Grids

6 questions. Covers Checkerboard and grid art.

Questions 1 to 5 of 6

  1. 1. This program prints a checkerboard. What does the second line of output look like? ``cpp #include <iostream> #include <string> std::string checkerboard(int rows, int cols, char dark, char light) { std::string picture; for (int row = 0; row < rows; ++row) { for (int col = 0; col < cols; ++col) { picture += ((row + col) % 2 == 0) ? dark : light; } picture += '\n'; } return picture; } int main() { std::cout << checkerboard(3, 3, '*', '-'); } ``
  2. 2. What does this program print? ``cpp #include <iostream> int count_dark(int rows, int cols) { int dark = 0; for (int row = 0; row < rows; ++row) { for (int col = 0; col < cols; ++col) { if ((row + col) % 2 == 0) { ++dark; } } } return dark; } int main() { std::cout << count_dark(4, 3) << '\n'; } ``
  3. 3. This checkerboard test is meant to alternate the cells, but it does not. Which part is the bug? ``cpp if (row + col % 2 == 0) { std::cout << '#'; } else { std::cout << '.'; } ` The first row of a 3 by 3 grid is #.#`, but the next two rows are all dots.
  4. 4. Why does the checkerboard use (row + col) % 2 and not row % 2?
  5. 5. The grid art draws a grid with 3 cells across. How many characters wide is each line of the picture?

5 question(s) left on this page.