[問題] 函式一問
開發平台(Platform): (Ex: VC++, Gcc, Linux, ...)
DEV CPP
額外使用到的庫(Library Used) (Ex: OpenGL, ...)
問題(Question):
這是一題課本的問題,輸入base跟exponent,輸出結果
餵入的資料(Input):
base,exponent
預期的正確結果(Expected Output):
result = base^exponent
錯誤結果(Wrong Output):
result只會輸出base
程式碼(Code): (請善用置底文標色功能)
#include<stdio.h>
#include<stdlib.h>
int integerPower(int base,int exponent);
int main(void)
{
int base;
int exponent;
int result;
printf("Please enter a base:\n");
scanf("%d",&base);
printf("Please enter a exponent:\n");
scanf("%d",&exponent);
printf("The result is %d\n",integerPower( base,exponent));
system("pause");
return 0;
}
int integerPower(int base,int exponent)
{
int i=1;
int result=1;
if (exponent = 0){
return 1;
}
else if (exponent = 1){
return base;
}
else
for(i=1;i<=exponent;i++)
{
result = result * base;
}
return result;
}
補充說明(Supplement):
請各位先進幫忙指出小弟什麼地方寫錯了,感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 202.111.1.107
→
01/10 19:41, , 1F
01/10 19:41, 1F
→
01/10 19:42, , 2F
01/10 19:42, 2F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章
11
38