【发布时间】:2018-11-09 20:49:55
【问题描述】:
我乞求你们的帮助,我不是最好的程序员,我花了很多时间在这上面,我很累很沮丧:[
基本上,我想将int 和 4 个单维数组传递给一个函数,该函数将返回一个指向数组(用于我的目的的解决方案向量)的指针,我在 C 中理解该指针,我可以通过静态命令在函数中定义.
基本上它说它不明白为什么将 double 类型数组传递给返回指向 double 的指针的函数,并且由于某种原因,它期望指针作为参数,正如我所理解的那样。 而且我认为它拖到了粗体线,因为存在兼容性问题,至少我认为这是导致该错误的原因。 请帮帮我:]
#include <stdio.h>
#define N 4
double* thomas_algorithm(int n, double c[], double b[], double a[], double
d[]);
int main()
{
int i, n;
double* p;
double a[N-1]={0}, b[N]={0}, c[N-1]={0}, d[N]={0};
printf("please enter the order of the coefficient matrix:\n");
scanf("%d", &n);
printf("please insert the vector c:\n");
for(i=0; i<N-1; i++)
{
scanf("%lf", &c[i]);
}
printf("please insert the vector b:\n");
for(i=0; i<N; i++)
{
scanf("%lf", &b[i]);
}
printf("please insert the vector a:\n");
for(i=0; i<N-1; i++)
{
scanf("%lf", &a[i]);
}
printf("please insert the vector d:\n");
for(i=0; i<N; i++)
{
scanf("%lf", &d[i]);
}
**p=thomas_algorithm(n, c[N-1], b[N], a[N-1], d[N]);**
for(i=0; i<N; i++)
{
printf("x(%d)=%f", i+1, p+i);
}
return 0;
}
double* thomas_algorithm(int n, double c[], double b[], double a[], double
d[]) {
int i;
static double x[N]={0};
for(i=1; i<n-1; i++) /*factorization phase*/
{
b[i]=b[i]-(a[i]/b[i-1])*c[i-1];
d[i]=d[i]-(a[i]/b[i-1])*d[i-1];
}
/*backward substitution*/
x[N-1]=d[N-1]/b[N-1];
for(i=n-2; i>-1; i++)
{
x[i]=(d[i]-c[i]*x[i+1])/b[i];
}
return x;
}
【问题讨论】:
-
p=thomas_algorithm(n, c[N-1], b[N], a[N-1], d[N]);->p=thomas_algorithm(n, c, b, a, d);。a[i]是特定元素,而a是数组本身(衰减为指针)。 -
@Osiris 没错!写一个答案!
标签: c++ c c89 numerical-analysis