如何交换身份矩阵的第一行和最后一行?

时间:2018-09-13 02:09:46

标签: c

我有以下任务: enter image description here

我在C语言中创建了一个身份矩阵。我需要交换第一行和最后一行。我不确定如何。这是我的代码:

#include<stdio.h>

int main() 
{ 

    int i;
    int j;
    for (i = 0; i < 5; i++) 
    { 
        for (j = 0; j < 5; j++) 
        { 
            //if i = j, print a 1
            if (i == j) 
                printf("%d ", 1);

            //else, print 0
            else
                printf("%d ", 0); 
        } 

        //use this to make it print on seprate lines
        printf("\n"); 


    } 
} 

这是我到目前为止所拥有的。尝试时,我会不断获得随机数。所以我不确定要更改什么:

    int main() 
{ 

    int i;
    int j;
    int x;
    int ar[i][j];
    int *i0, *i4, *temp;
    for (i = 0; i < 5; i++) 
    { 
        for (j = 0; j < 5; j++) 
        { 
            if(i=j){
                printf("%d",1);

            }
            else{
                printf("%d",0);
            }



        i0 = &(ar[i][j]);  
        i4 = &(ar[i][j]);
        temp = i0;  
        i0 = i4;  
        i4 = temp; 
        for(x=0;x<5;x++) {   
            printf(" %d", *(i0 + x));  
            }

        printf("\n"); 

        } 
    }
}

1 个答案:

答案 0 :(得分:0)

  

我在C中创建了一个单位矩阵。我需要交换第一个和   最后一行。我不确定如何。

#include<stdio.h>

int main() 
{ 

    int i;
    int j;
    for (i = 0; i < 5; i++) 
    { 
        for (j = 0; j < 5; j++) 
        { 
            //if i = j, print a 1
            if (i == j) 
                printf("%d ", 1);

            //else, print 0
            else
                printf("%d ", 0); 
        } 

        //use this to make it print on seprate lines
        printf("\n"); 


    } 
}

这里没有矩阵。您只需在i != j时打印整数0,在i == j时打印整数1。

  

这是我到目前为止所拥有的。我尝试时不断获得随机数   它。所以我不确定要更改什么

int main() 
{ 

    int i;
    int j;
    int x;
    int ar[i][j];
    int *i0, *i4, *temp;
    for (i = 0; i < 5; i++) 
    { 
        for (j = 0; j < 5; j++) 
        { 
            if(i=j){
                printf("%d",1);

            }
            else{
                printf("%d",0);
            }



        i0 = &(ar[i][j]);  
        i4 = &(ar[i][j]);
        temp = i0;  
        i0 = i4;  
        i4 = temp; 
        for(x=0;x<5;x++) {   
            printf(" %d", *(i0 + x));  
            }

        printf("\n"); 

        } 
    }
}
  1. int iint j未初始化,所以int ar[i][j]的尺寸是多少?
  2. int ar[i][j]未初始化,那么位于矩阵任何给定元素上的是什么?
  3. 您似乎认为您在使用指针在做一些有用的事情:

        i0 = &(ar[i][j]);  
        i4 = &(ar[i][j]);
        temp = i0;  
        i0 = i4;  
        i4 = temp;
    

    但这是您真正在做什么:

        /* i0 points to the memory location of the matrix element ar[i][j] */
        i0 = &(ar[i][j]);
    
        /* i4 points to the memory location of the matrix element ar[i][j] */ 
        i4 = &(ar[i][j]);
    
        /* temp points to where i0 points...
         * which is the memory location of the matrix element ar[i][j]
         */
        temp = i0;
    
        /* i0 points to where i4 points...
         * which is the memory location of the matrix element ar[i][j]
         */
        i0 = i4;
    
        /* i4 points to where temp points...
         * which is where i0 points which is where i4 points
         * which is the memory location of the matrix element ar[i][j]
         */
        i4 = temp;
    

    严重的是,这里没有完成任何有用的工作,所有指向矩阵元素ar[i][j]的指针

  4. 然后您执行此操作:

        for(x = 0; x < 5; x++)
        {   
            printf(" %d", *(i0 + x));  
        }
    

    表示打印i0所指向的内存位置的内容加上偏移量x ...但是i0指向未初始化的矩阵元素,因此也就不足为奇了得到“随机数”。

实际上,您需要创建一个矩阵并修改其内容:

int ar[5][5];

/* assign elements of matrix, make it an identity matrix */
for(int i = 0; i < 5; i++){
    for(int j = 0; j < 5; j++){
        if(i == j){
            ar[i][j] = 1;
         }
         else ar[i][j] = 0;
    }
}

/* print elements of matrix */
for(int i = 0; i < 5; i++){
    for(int j = 0; j < 5; j++){
        printf("%d ", ar[i][j]);
    }
    printf("\n");
}

/* pointer to the first element of the matrix */
int * ptr = &ar[0][0];

/* temp variables used in swap */
int temp1 = 0;
int temp2 = 0;

/* swap first and last row */
for(int i = 0; i < 5; i++){
    temp1 = *(ptr + i);         //ar[0][i]
    temp2 = *(ptr + 4*5 + i);   //ar[4][i]
    *(ptr + i) = temp2;         //ar[0][i] = ar[4][i]
    *(ptr + 4*5 + i) = temp1;   //ar[4][i] = ar[0][i]
}

printf("\n");

/* print elements of matrix */
for(int i = 0; i < 5; i++){
    for(int j = 0; j < 5; j++){
        printf("%d ", ar[i][j]);
    }
    printf("\n");
}