## Trending News

# c語言x的y次方,請問我哪裡寫錯?

題目:

Write a power(x, y) function which can take 0 as its arguments

Declare a 7x7 array called myPower[ ][ ].

Store the value of xy in myPower[x][y] by calling the power(x, y) function.

Print them out.範例 (Example):a[0][0] = 1 (=0^0)

a[0][1] = 0 (=0^1)

a[0][2] = 0 (=0^2)

...

a[0][6] = 0 (=0^6)

a[1][0] = 1 (=1^0)

a[1][1] = 1 (=1^1)

...

a[6][5] = 7776 (=6^5)

a[6][6] = 46656 (=6^6) Note that y is assumed to be an integer and >= 0.

The definition of power function is: x 0 = 1 no matter what x is; 0 y = 0 if y ≠ 0; x y otherwise. 以下是我寫的程式(跑出來有錯): #include<stdio.h>

#include<stdlib.h>

int power(int x,int y){

int answer=1,i;

if(y==0){

return answer;

}

else if(y>0){

for(i=0;i<y;i++){

answer*=x;

return answer;

}

}

}

int main(){

int m,n,mypower[7][7];

for(m=0;m<7;m++){

for(n=0;n<7;n++){

mypower[m][n]=power(m,n);

printf("mypower[%d][%d]= %d (=%d^%d)\n",m,n,mypower[m][n],m,n);

}

}

system("pause");

return 0;

}

請問哪裡有錯?

麻煩解釋一下意義

感謝!!

### 1 Answer

- 9 years agoFavorite Answer
如果在括弧內, 算一次就跳出來了

for ( i = 0; i < y; i++ )

{

answer *= x;

// return asnwer; <<< wrong place.

}

return answer;