C语言实现拉格朗日插值法的问题

#include <stdio.h>
#include <stdlib.h>

void main( void )
{
int n;
float *x = NULL;
float *y = NULL;
float xFound;
float yGet = 0.0;
float yGetTemp = 1.0;
int client;
int temp;

printf("input how much x you wanna input,o~~");
scanf("%d",&n);
getchar();

printf("n=%d",n);
getchar();

x=(float *)malloc(sizeof(float)*n);

for (client=0;client<n;client++)
{
printf("input the x's date");getchar();
scanf("%f",(x+client));
}

y=(float *)malloc(sizeof(float)*n);

for (client=0;client<n;client++)
{
printf("input the y's date");getchar();
scanf("%f",(y+client));
}

for (client=0; client < n; client++)
{
for (temp = 0; temp < n; temp++)
{

if (temp == client)
continue;

else
yGetTemp *= ( (xFound - *(x + temp) ) / ( *(x + client) - *(x + temp) ) );
}

yGet += (yGetTemp * (*(y + client)));
yGetTemp = 1.0;
}
printf("The result is:%f", yGet);
}

我的程序一到输入X值那里就跳出来了,我感觉是内存分配问题,求解释啊!

下面是我所写的拉格朗日C语言实现的程序,经运行正确

#include<stdio.h>
int main()
{
float x,y;
float a[3]={100,121,144};
float b[3]={10,11,12};
int i,j,k=0;
float t;
y=0;
printf("请输入x的值:");
scanf("%f",&x);
for(k=0;k<3;k++)
{
t=1;
for(j=0;j<=2;j++)
if(j!=k)
{
t=((x-a[j])/(a[k]-a[j]))*t;
}
y=y+t*b[k];
}
printf("y=%f\n",y);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-04-08
貌似你的xFound 值没有初始化就用 肯定会出问题的。
第2个回答  2010-04-08
没有跳出来!
第3个回答  2010-04-08
把getchar 去了看下。
相似回答