Embercode
✕
C++ PatternsQuick quiz

Triangles

8 questions. Covers Floyd's triangle.

Questions 1 to 5 of 8

  1. 1. What does this program print? ``cpp #include <iomanip> #include <iostream> #include <string> int main() { const int rows = 3; const int last = rows * (rows + 1) / 2; const int width = static_cast<int>(std::to_string(last).size()); int next = 1; for (int row = 1; row <= rows; ++row) { for (int col = 1; col <= row; ++col) { if (col > 1) std::cout << ' '; std::cout << std::setw(width) << next; ++next; } std::cout << '\n'; } } ``
  2. 2. What does this program print? ``cpp #include <iostream> long long binomial(int n, int k) { long long result = 1; for (int i = 1; i <= k; ++i) result = result * (n - k + i) / i; return result; } int main() { std::cout << binomial(7, 3) << ' ' << binomial(7, 0) << ' ' << binomial(6, 2) << '\n'; } ``
  3. 3. What does this program print? ``cpp #include <iostream> #include <vector> int main() { const int rows = 5; std::vector<long long> row = {1}; std::cout << "row sums:"; for (int r = 0; r < rows; ++r) { long long sum = 0; for (long long v : row) sum += v; std::cout << ' ' << sum; row.push_back(1); for (int c = static_cast<int>(row.size()) - 2; c >= 1; --c) row[c] += row[c - 1]; } std::cout << '\n'; } ``
  4. 4. Row 10 of Pascal's triangle adds up to 2 raised to the power ___. Fill in the exponent.

    Type your answer.

  5. 5. Store the values as ___ so that the middle value of row 34 does not overflow.

    Type your answer.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.