LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 767|回复: 4

很久没有编程了,请看一下这个程序的指针调用如何改正.

[复制链接]
发表于 2005-6-24 16:19:57 | 显示全部楼层 |阅读模式
#include<iostream.h>

/* This function uses Gauss-elimination method
   to solve multi-variable  */
void Gauss(double *A,         /* A is nXn order matrix */
           double *b,         /* b is nX1 order matrix */
           double *x,         /* x is nX1 order matrix */
           int n              /* row and column of A */
           )
{
  int i,j,k;                  /* indices */
  double temp;

  for(i=0;i<n;i++)              /* convert A to top-triangle matrix */
    {
      for(k=i+1;k<n;k++)
        for(j=i;j<n;j++)
          {
            temp=A[k]/(A);
            A[k][j]=(A[k][j])-temp*(A[j]);
            b[k]=b[k]-temp*b;
          }
    }

  for(i=n-1;i>=0;i--)                /* get the solution of x */
    {
      if(i-n-1) x=b/A;
      else
        {
          for(j=i+1;j<n;j++)
            temp+=A[j]*x[j];
          x=(b-temp)/A;
        }
    }
  
}

int main()        /* main part of program */
{
  int n;        /* row and column of A or row of b */
  int i,j;        /* indexes */

  cout<<"请输入n:";
  cin>>n;

  double A[n][n],b[n],x[n],(*pa)[n],*pb,*px;        /* define matrixes */
  pa=A;
  pb=b;
  px=x;  

  cout<<"请输入A:\n";
  for(i=0;i<n;i++)
    for(j=0;j<n;j++)
      cin>>A[j];

  cout<<"请输入b:"<<endl;
  for(i=0;i<n;i++)
    cin>>b;

  Gauss(pa,pb,px,n);

  for(i=0;i<n;i++)
    cout<<x<<endl;
}
发表于 2005-6-24 22:28:33 | 显示全部楼层
(*pa)[n] -->*pa[n]
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-6-24 22:44:55 | 显示全部楼层
不行.关键在函数调用那里出错了.那个二维数组怎么调用啊.
回复 支持 反对

使用道具 举报

发表于 2005-6-25 00:57:56 | 显示全部楼层
Sorry, 我刚刚在回答这个问题的时候,也没有动脑子
#define GET(A,i,j,n)     A[i*n + j]
定义的时候应该是
void Gauss(double *A, /* A is nXn order matrix */
double *b, /* b is nX1 order matrix */
double *x, /* x is nX1 order matrix */
int n /* row and column of A */
)
然后将这个函数定义内的A[j]全部改成GET(A,i,j,n)

调用的时候: Gauss(A, b, x, n)

或者使用gcc的扩充
这样定义函数
void Gauss(int n; /*forward declaration http://gcc.gnu.org/onlinedocs/gc ... tml#Variable-Length
*/
double A[n][n], /* A is nXn order matrix */
double *b, /* b is nX1 order matrix */
double *x, /* x is nX1 order matrix */
int n /* row and column of A */
)
回复 支持 反对

使用道具 举报

发表于 2005-6-25 04:27:17 | 显示全部楼层
所有楼上各位大哥:能不能用[ CODE ][ /CODE ]把代码格式化好看一点啊?会不会用啊?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表