6 questions. Covers Checkerboard and grid art.
Questions 1 to 5 of 6
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, '*', '-'); }
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'; }
cpp if (row + col % 2 == 0) { std::cout << '#'; } else { std::cout << '.'; }
The first row of a 3 by 3 grid is
%
+
else
#
.
<=
row
col
(row + col) % 2
row % 2
5 question(s) left on this page.