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);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.