How to return matrix?

雨燕双飞 提交于 2019-12-25 09:17:19

问题


I'm trying to make a Cramer-linear-solving, and I wrote a function that replaces a matrix column, just like that:

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

But it is currently void, and I want it to return the mat_out with it's values, when I call this function... How could I do that??


回答1:


You can avoid to use 2 matrices for your function. You can simply:

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] it is a pointer float(*)[] so modifications on that parameter are made on the passed matrix.



来源:https://stackoverflow.com/questions/42368262/how-to-return-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!