Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Re-write the crummy code with: #include <stdio.h> int x, n, result; int power(in

ID: 2989576 • Letter: R

Question

Re-write the crummy code with:

#include <stdio.h>

int x, n, result;

int power(int x, int n);

int main(int argc, char *argv[]){

                x = 3;

                n = 4;

                result = power(x, n);

                // Print it out

            printf("%d ", result);

}

/* raise int x to n-th power, n>0 */

int power( int x, int n )

{

                int p;

               

                for (p = 1; n > 0; --n)

                                {

                                p = p * x;

                                }

                return(p);

}

Explanation / Answer

#include <stdio.h>

int x, n, result;

int power(int x, int n);

int main(int argc, char *argv[]){

                x = 3;

                n = 4;

                result = power(x, n);

                // Print it out

            printf("%d ", result);

/* raise int x to n-th power, n>0 */

int power( int x, int n )

{

                int p;

               

                for (p = n; n > 0; --n)

                                {

                                p = p * x;

                                }

                return(p);

}

}