繁体   English   中英

我在以下代码中遇到了问题,这些代码具有不同的变量类型和用于查找 Bessel 函数的指针

[英]I have trouble with the following code with different variable types and pointers for finding Bessel functions

这个作业是我的实验室之一,它希望我们根据输入的用户值找到一阶贝塞尔 function 的值。 这么久了,我的心态是:先让用户输入,然后用数字的四舍五入,上面的数字和下面的数字。 之后,我根据数字进行插值,找到错误,然后将所有内容呈现在屏幕上。 (我们必须使用指针和 arrays 否则我们将不会得到任何分数。这是有道理的,我们如何列出从 0 到 10 的每个舍入的每个案例......)

问题是,我似乎没有得到正确的指针,并且程序总是说我们有 long int 和 *double 混合,即使我明确告诉程序这是一个 double。

代码:

#include <stdio.h>
#include <math.h>
#include <Stdlib.h>

int y;

 int readdata(double rho[y], double Jrho[y])
{

double a, b, c, x;

FILE* fp;
fp= fopen("Bessel1.dat", "r");

for (y=0; y<=10; y++)
    {
        fscanf(fp, "%lf %lf", &rho[y], &Jrho[y]);
    }

}

double Lagrange2nd(int y, double rho[y], double Jrho[y], double x)
{

double a, b, c;

double output;

a=(x-rho[(y+1)])*((x-rho[y])*Jrho[((y-1))]+(rho[((y-1))]- 

x)*Jrho[y])/(rho[((y-1))]-rho[y]);

b=(((rho[(y-1)]-x)*((x-rho[(y+1)])*Jrho[y]+(rho[y]- 

x)*Jrho[(y+1)]))/(rho[y]-rho[(y+1)]));

output = c = (a+b)/(rho[(y-1)]-rho[(y+1)]);

return output;
}

double errx(int y, double rho[y], double Jrho[y], double x)
{
double a, b, c;

double output;

output= (abs(c-(Jrho[(y-1)]*(x-rho[y])+Jrho[y]*(rho[(y-1)]-x)))+abs(c- 
(Jrho[y]*(x-rho[(y+1)])+Jrho[(y+1)]*(rho[y]-x))))/2;

return output;

}

int main()
{
double x, Jrho[y], rho[y];

printf("x=");
scanf("%lf", &x);
y=round(x);

Lagrange2nd(&rho[y], &x, &Jrho[y], y);

errx(&Jrho[y], &x, &rho[y], y);

printf("%d, %d", Lagrange2nd, errx);

return(0);
}

我知道这很麻烦,但我直到 2 个月前才知道任何编程,但我们现在必须这样做......哎呀,我不太擅长这个,是吗?

你用什么编译器? 使用 gcc 您会收到很多警告。 特别是,您几乎没有声明返回 void 但返回 int 的函数。

看起来您的编译器确实将 'void' 视为 'int' - 这会导致很多精度问题。 如果您已关闭警告 - 考虑重新启用它们,或使用更好的编译器:-)

void errx(double* rho[y], double* Jrho[y], double* x)
{
...
double output;

output= (abs(c-(Jrho[(y-1)]*(x-rho[y])+Jrho[y]*(rho[(y-1)]-x)))+abs(c-(Jrho[y]*(x- 
rho[(y+1)])+Jrho[(y+1)]*(rho[y]-x))))/2;

return output;
}

样式说明:考虑在一个语句中组合声明和赋值。 它将减少许多可能的错误,并使您获得更好的成绩!

double output = (abs(c-(Jrho[(y-1)]*(x-rho[y])+Jrho[y]*(rho[(y-1)]-x)))+abs(c-(Jrho[y]*(x-rho[(y+1)])+Jrho[(y+1)]*(rho[y]-x))))/2;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM