/** *************************************************************************** * @remark After reading a double x and non-negative integer n * * program assigns and prints an array with values * * * * a[i] = i^i for 1 <= i <= n * * * * program uses both iterative and recursive procedures, * * to provide a context for efficiency analysis for both types * * of procedures. * * * * compile with * * gcc -o simple-loop-analysis-3 simple-loop-analysis-3.c * * * * @author Henry M. Walker * * @file simple-loop-analysis-3.c * * @date Summer, 2023 * * @date November 25, 2024 (revised) * * * *****************************************************************************/ /** */ #include /* required computation done iterative in nested loops */ void iter_compute (int n, long a[ ]) { int i, j; for (i = 0; i <= n; i++) { a[i] = 1; for (j = 0; j < i; j++) { a[i] *= i; } } } /* required computation done recursively for n */ void rec_compute (int n, long a[ ]) { a[n] = 1; if (n > 0) { for (int j = 0; j < n; j++) { a[n] *= n; } rec_compute (n-1, a); } } /* print a given an array */ void print_array (int n, long a[ ]) { int i; for (i = 0; i <= n; i++) printf ("%7ld ", a[i]); printf ("\n"); } int main () { /* prolog */ printf ("program to compute and print array\n"); printf (" a[i] = i^i for 0 <= i <= n\n"); /* determine array size */ int n; printf ("enter non-negative integer n: "); scanf ("%d", &n); long r[n], s[n]; /* iterative processing */ iter_compute (n, r); printf ("result via iteration\n"); print_array (n, r); /* recursive processing */ rec_compute ( n, s); printf ("result via recursion\n"); print_array (n, s); return 0; }