在C语言中交换方阵名称

在C语言中交换方阵名称,c,pointers,matrix,swap,C,Pointers,Matrix,Swap,我一直在寻找一种在C语言中两个矩阵之间交换名称的方法。我有两个正方形x大小的矩阵。我对其中一个进行一些运算,将结果放入另一个矩阵中的一个单元格中,然后交换它们的名称,然后重复 下面是我的代码 int main(void){ int const size = 1000; int const steps = 10; float A[size][size], B[size][size]; int i,j,k; int t = 0; double sum = 0; double sum1 = 0; in

我一直在寻找一种在C语言中两个矩阵之间交换名称的方法。我有两个正方形x大小的矩阵。我对其中一个进行一些运算,将结果放入另一个矩阵中的一个单元格中,然后交换它们的名称,然后重复

下面是我的代码

int main(void){
int const size = 1000;
int const steps = 10;
float A[size][size], B[size][size];
int i,j,k;
int t = 0;
double sum = 0;
double sum1 = 0;
int const ed = size - 1;
for(i = 0; i < size; ++i){
    for(j = 0; j < size; ++j){// initialize the matrices
        A[i][j] = i+j;
        B[i][j] = 0;
    }
}
for(i = 0; i < size; ++i){//find the sum of the values in the first matrix
    for(j = 0; j < size; ++j){
        sum = sum + A[i][j];
    }
}
printf("The total sum of the matrix 1 is %lf \n",sum);

for(k = 0; k < steps; ++k){//for each cell of the matrix A calculate the average of the values' of the cell and its surroundings and put it in the coresponding place in the matrix B and then copy matrix B to matrix A and repeat. There are special cases for the cells who are at the edges and the last or first row/column.
    for(i = 0; i < size; ++i){
        for(j = 0; j < size; ++j){
            if(i==0){
                if(j==0)
                    B[i][j]=(A[0][0]+A[0][1]+A[0][ed]+A[1][0]+A[ed][0])/5.0;
                else if(j==ed)
                    B[i][j]=(A[0][ed]+A[0][0]+A[0][ed-1]+A[1][ed]+A[ed][ed])/5.0;
                else
                    B[i][j]=(A[0][j]+A[0][j+1]+A[0][j-1]+A[1][j]+A[ed][j])/5.0;
            }else if(i==ed){
                if(j==0)
                    B[i][j]=(A[ed][0]+A[ed][1]+A[ed][ed]+A[0][0]+A[ed-1][0])/5.0;
                else if(j==ed)
                    B[i][j]=(A[ed][ed]+A[ed][0]+A[ed][ed-1]+A[0][ed]+A[ed-1][ed])/5.0;
                else
                    B[i][j]=(A[ed][j]+A[ed][j+1]+A[ed][j-1]+A[0][j]+A[ed-1][j])/5.0;
            }else{
                if(j==0)
                    B[i][j]=(A[i][0]+A[i][1]+A[i][ed]+A[i+1][0]+A[i-1][0])/5.0;
                else if(j==ed)
                    B[i][j]=(A[i][ed]+A[i][0]+A[i][ed-1]+A[i+1][ed]+A[i-1][ed])/5.0;
                else
                    B[i][j]=(A[i][j]+A[i][j+1]+A[i][j-1]+A[i+1][j]+A[i-1][j])/5.0;
            }
        }
    }
    sum1 = 0;
    for(i = 0; i < size; ++i){
        for(j = 0; j < size; ++j){
            sum1 = sum1 + B[i][j];
        }
    }
    t=t+1;
    for(i = 0; i < size; ++i){
        for(j = 0; j < size; ++j){
            A[i][j] = B[i][j];
        }
    }
    printf("%lf \n",sum1-sum);
}
printf("The total sum of the matrix 2 is %lf \n",sum1);
printf("Number of steps completed: %i \n",t);
printf("Number of steps failed to complete: %i \n", steps-t);
return 0;
int main(无效){
int const size=1000;
int const steps=10;
浮动A[大小][size],B[大小][size];
int i,j,k;
int t=0;
双和=0;
双sum1=0;
int const ed=大小-1;
对于(i=0;i
}

我使用了每次将一个矩阵复制到另一个矩阵的方法,但这不是有效的


我有一个暗示,我应该使用指针,但我想不出来。任何帮助都将不胜感激。

是的,您一定要使用指针,例如:

void swap (int*** m1, int*** m2)
{
    int** temp;
    temp = *m1;
    *m1 = *m2;
    *m2 = temp;
}
然后调用:

int m1[5][5] = 0;
int m2[5][5] = 0;
swap (&m1, &m2);

通过将第一个变量的值分配给临时变量,然后将第二个变量的值分配给第一个变量,然后将临时变量的值分配给第二个变量,可以交换相同类型的任意两个变量的值:

int a = 2, b = 3, tmp;

tmp = a;
a = b;
b = tmp;
特别是,当变量是指针类型时,它的工作原理完全相同,因此

/* The matrices */
double one[3][3], another[3][3];

/* pointers to the matrices */
double (*matrix1p)[3] = one;
double (*matrix2p)[3] = another;
double (*tmp)[3];

/* ... perform matrix operations using matrix1p and matrix2p ... */

/* swap labels (pointers): */
tmp = matrix1p;
matrix1p = matrix2p;
matrix2p = tmp;

/* ... perform more matrix operations using matrix1p and matrix2p ... */

更新以澄清:

matrix1p
最初是一个
one
的别名,
matrix2p
最初是另一个
的别名。交换后,
matrix1p
是另一个
的别名,而
matrix2p
是一个
的别名。当然,您可以根据需要多次执行此类交换。但是,除非通过逐个元素交换,否则不能交换
一个
另一个
本身



请注意,这会提高效率,因为指针相对于矩阵本身非常小。您不必移动矩阵的元素,只需更改每个指针所指的矩阵。

这看起来很方便,但实际上不起作用。(a) 您正在将
int(*)[5][5]
传递给一个需要
int***
且类型不匹配的函数,并且(b)您不能更改这样的数组。感谢您的努力,Michal,但由于不匹配,它无法工作。我试图理解您的建议:在完成这些步骤之后,当我提到另一个[I][j]元素时,它是否与交换前的[I][j]元素相同?@user2149122,否。
one
one
的元素不会移动,这正是问题所在。而且,交换的不是“一个”和“另一个”的名字;相反,名称“matrix1p”和“matrix2p”被(有效地)交换。更严格地说,交换的是后一个名称所指的矩阵。如果你必须移动矩阵元素,就没有效率了。好吧。您还有一个问题:如何使用指针访问特定元素One[I][j?@user2149122,考虑到我给出的声明,
One
matrix1p
别名,您可以访问
One[I][j]
作为
matrix1p[I][j]
。就这么简单。同样地,虽然
one
的别名为
matrix2p
,但您可以访问
one[i][j]
作为
matrix2p[i][j]
。我在问题中发布了我的代码,以便更好地理解。当一个步骤完成时,矩阵B的值发生了什么并不重要,因为它们将被下一步的平均值所取代。(另一个minnor问题是,该代码在单元格的值之和应该是常量的方面不起作用,但当我运行它时,会出现一些小的分隔。如果您能看一下,我将非常感谢)。我的问题