2017-02-21 111 views
0

我试图做一个克拉默线性解决了,我写了替代矩阵列的功能,就像这样:如何返回矩阵?

void replacecol(int c, int n, float mat_in[n][n], float vect_in[n], 
     float mat_out[n][n]) 
{ 
    int i, j; 

    for (i = 0; i < n; i++) 
    { 
     for (j = 0; j < n; j++) 
     { 
      if (j == c) 
      { 
       mat_out[i][j] = vect_in[j]; 
      } 
      else 
      { 
       mat_out[i][j] = mat_in[i][j]; 
      } 
     } 
    } 
} 

但它目前是无效的,而且我希望它返回mat_out与它的值,当我调用这个函数...我怎么能这样做?

+4

你为什么要退货?你需要从中获得什么? – StoryTeller

+2

你不能用C返回数组。函数已经正确设置了 –

+2

好吧,你不能返回数组/矩阵,但你可以返回指针。 – LPs

回答

6

您可以避免为您的功能使用2个矩阵。你可以简单:

void replacecol(int c, int n, float mat_in[n][n], float vect_in[n])) 
{ 
    int i; 

    for (i = 0; i < n; i++) 
    { 
     mat_in[i][c] = vect_in[i]; 
    } 
} 

float mat_in[n][c]它是如此对参数的修改都在通过基质制成的指针float(*)[]

+0

哇,我不知道replacecol()会修改传递的矩阵本身......我认为它就像在Java中那样,需要一个返回来实现...非常感谢! –

+0

可能指针的力量与你(并研究一些关于他们......);) – LPs