/* debug_this.c Jeff Ondich, 27 March 2023 A simple program for use with our VS Code debugging lab. The Nth triangular number is the sum 1 + 2 + ... + N. So T[1] = 1, T[2] = 3, T[3] = 6, etc. Yes, there is a closed-form formula (T[N] = N * (N - 1) / 2), but we're ignoring it so we can watch loops and recursion in action. This program includes an off-by-one error. */ #include #include int iterative_triangular_number(int n); int recursive_triangular_number(int n); int main(int argc, char *argv[]) { int limit = 10; printf("Here are the first %d triangular numbers, recursively computed:\n", limit); for (int k = 1; k <= limit; k++) { int t = recursive_triangular_number(k); printf(" T(%d) = %d\n", k, t); } printf("\n"); printf("Here are the first %d triangular numbers, iteratively computed:\n", limit); for (int k = 1; k <= limit; k++) { int t = iterative_triangular_number(k); printf(" T(%d) = %d\n", k, t); } printf("\n"); return 0; } // Returns the nth triangular number, or 0 if n <= 0. int iterative_triangular_number(int n) { int total = 0; for (int k = 0; k < n; k++) { total = total + k; } return total; } // Same, but recursive. int recursive_triangular_number(int n) { if (n < 1) { return 0; } int t = recursive_triangular_number(n - 1); t = t + n; return t; }