在C中交换方形矩阵名称

时间:2015-02-11 20:33:15

标签: c pointers matrix swap

我一直在寻找一种在C中交换两个矩阵之间名称的方法。我有2个方形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;

}

我曾经使用过每次将一个矩阵复制到另一个矩阵的方法,但效率不高。

我有一个提示,我应该使用指针,但我无法弄清楚。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

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

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最初是another的别名。交换后,matrix1panother的别名,而matrix2pone的别名。当然,您可以根据需要多次执行此类交换。但是,您不能自己交换oneanother,除非通过逐个元素交换。


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

答案 1 :(得分:0)

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

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);