/* pingala.cpp * Created: 2024-09-15 * Author: Aryadev Chavali */ #include #include #include #include #include std::vector generate_triangle(const size_t depth) { std::vector levels; std::vector items; std::stringstream ss; levels.reserve(depth); items.resize(depth * depth); #define AT(i, j) items[((i) * depth) + (j)] AT(0, 0) = 1; levels.push_back("1"); for (size_t i = 1; i < depth; ++i) { AT(i, 0) = 1; ss << "1 "; for (size_t j = 1; j < i; ++j) { AT(i, j) = AT(i - 1, j - 1) + AT(i - 1, j); ss << AT(i, j) << " "; } AT(i, i) = 1; ss << "1"; levels.push_back(ss.str()); ss.str(std::string{}); } #undef AT return levels; } void usage(FILE *fp) { fprintf(fp, "Usage: pingala.out [depth]\n" "\tdepth: Depth of triangle generated\n"); } int main(int argc, char *argv[]) { // Variable declarations std::vector levels; int depth = 0; if (argc < 2) goto error; depth = std::stoi(argv[1]); if (depth <= 0) goto error; levels = generate_triangle(depth); for (const auto &level : levels) { for (size_t i = 0; i < (levels[levels.size() - 1].size() - level.size()) / 2; ++i) { printf(" "); } printf("%s\n", level.c_str()); } return 0; error: usage(stderr); return 1; }