所以我正在編寫一個程序,使用Horner的規則來計算多項式。Horner的規則C
但是當我輸入第一個係數後程序崩潰了。我做錯了什麼?我找不到錯誤。
編輯:我只是注意到,我正在讀的參數向後。
int main() {
int degree;
float x;
float px = 0;
float p = 0;
float *a;
int i;
a = malloc((degree+1)*sizeof(float));
printf("Enter the degree:");
scanf("%d", °ree);
printf("Enter the argument:");
scanf("%f", &x);
for (i = 0; i < degree+1; i++)
{
printf("Enter the coefficient Nr%d:", i+1);
scanf("%f", *(a+i));
}
for (i = degree; i > -1; i--)
{
p = *(a+i) * pow(x, i);
px + p;
}
printf("%f", px);
return 0;
}
'px + p;'assign to where? – Maroun
我假設你在輸入'degree'後打算分配'a'? – Xymostech
'p = *(a + i)* pow(x,i);'不是霍納的方法,你的意思是'px + = ...' –