简体   繁体   English

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

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

This assignment is one of my labs and it wants us to find the value of the 1st order Bessel function according to the input-ed user value.这个作业是我的实验室之一,它希望我们根据输入的用户值找到一阶贝塞尔 function 的值。 So long, my mindset is: first I will ask the user for input, then I use the number's rounding, the number above and the number below.这么久了,我的心态是:先让用户输入,然后用数字的四舍五入,上面的数字和下面的数字。 After that, I perform the interpolation according to the numbers, find the error, then present everything on screen.之后,我根据数字进行插值,找到错误,然后将所有内容呈现在屏幕上。 (We must use pointers and arrays else we will not get any marks. That makes sense, how are we gonna list out every case for every rounded number from 0 to 10......) (我们必须使用指针和 arrays 否则我们将不会得到任何分数。这是有道理的,我们如何列出从 0 到 10 的每个舍入的每个案例......)

The problem is, I seem to not get the pointers right, and the program always say that we have long int and *double mixed, even though I explicitly told the program that this is a double.问题是,我似乎没有得到正确的指针,并且程序总是说我们有 long int 和 *double 混合,即使我明确告诉程序这是一个 double。

Code:代码:

#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]); 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)])); 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);
}

I know this is messy, but I did not know any programming until 2 months ago, yet we now have to do this......gee, I am not very good at this, am I?我知道这很麻烦,但我直到 2 个月前才知道任何编程,但我们现在必须这样做......哎呀,我不太擅长这个,是吗?

What compiler do you use?你用什么编译器? with gcc you will get MANY warnings.使用 gcc 您会收到很多警告。 In particular, you have few functions that declare to return void, but return an int.特别是,您几乎没有声明返回 void 但返回 int 的函数。

Looks like you compiler does treat 'void' as 'int' - which will cause lot of precision issue.看起来您的编译器确实将 'void' 视为 'int' - 这会导致很多精度问题。 If you have turned of warning - consider re-enabling them, or use better compiler:-)如果您已关闭警告 - 考虑重新启用它们,或使用更好的编译器:-)

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;
}

Style note: consider combing declaration and assignment in one statement.样式说明:考虑在一个语句中组合声明和赋值。 It will reduce many possible errors, and will get you better grade!它将减少许多可能的错误,并使您获得更好的成绩!

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