1
端末からユーザ配列へのユーザ入力の読み込みに問題があります。Cの1D配列へのscanf()
配列 'a'は動的サイズを持ちます。ユーザーが入力した多項式は、配列のサイズを決定します。
は、一度コンパイルして実行します。デバッグ行に
Enter the order number:
3
Enter your constant:
-90
Enter coefficient # 0
8
Enter coefficient # 1
4
Enter coefficient # 2
35
Enter coefficient # 3
54
0 8.000000
1 4.000000
2 0.000000
3 0.000000
は、私は戻って、ユーザへの配列を報告しています。いくつかの理由から、配列の後半にはゼロが返されます。私は問題が何であるかを理解できません。どんな助けでも大歓迎です。
PS。評価関数を無視する。ここ
は、私が働いているコードです:
//import required libraries
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// function prptotype
double eval(double a[], double x, int n); //n is max degree
//global variables
int N = 0;//N is the polynomial order
double *a;//array
double x; // constant
//main function
int main()
{
printf("%s\n", "Enter the order number:");
scanf("%d", &N); // user input for the order numbers
while (N < 1) //input debuger
{
printf("%d %s\n%s\n", N,"is NOT a positive and non-zero number", "Enter a positive and non-zero integer:");
scanf("%d", &N); // user input for the order numbers
}
a = malloc ((N + 1) * sizeof(int));// assigning the array size in respect with user input
printf("%s\n", "Enter your constant:");
scanf("%lf", &x);// user input for "x" constant
for (int i = 0; i < N + 1; ++i)
{
printf("Enter coefficient # %d\n", i);
scanf ("%lf", &a[i]);
}
/* Debug */
for (int i = 0; i < N + 1; ++i)
{
//a[i] = 0;
printf("%3d%13lf\n", i, a[i]);
}
}
//eval function
double eval(double a[], double x, int n)
{
}
'printf'では、'%f'または '%lf'を使用することができますが、違いはありません。 – dreamlax
@dreamlax C89は 'printf'で'%lf'をサポートしていません。 –
@CoolGuy:C89も27歳です。 POSIX、C99、C11はすべて許可しています。あなたが絶対にしなければC89を使っている点はありません。 – dreamlax