-2

我有一个 C 代码来生成对角单位矩阵。代码如下:

#include<stdio.h>
    
void main()
{
    int n,m,a,b,c,d,i,j;
    int ar1[100][100];
      
    printf("Enter value of n to create a square matrix of order n (odd no.): ");
    scanf("%d", &n);

    printf(" matrix in diagonal direction is - \n");
    for (i=0; i<n; i++) {
        for (j=0; j<n; j++) {
            if (ar1[j]==ar1[i]) {
                ar1[j][i] = 2;
                printf("%d, ", ar1[j][i]);
            } else {
                ar1[j][i] = -1;
                printf("%d, ", ar1[j][i]);
            }
        }
        printf("\n");
    }
}

这给了我这样的结果:

 2, -1, -1, -1, -1, 
-1,  2, -1, -1, -1, 
-1, -1,  2, -1, -1, 
-1, -1, -1,  2, -1, 
-1, -1, -1, -1,  2

但我想像这样以相反的方向打印对角矩阵:

-1, -1, -1, -1,  2, 
-1, -1, -1,  2, -1, 
-1, -1,  2, -1, -1, 
-1,  2, -1, -1, -1, 
 2, -1, -1, -1, -1

有人可以帮助我,关于如何以相反的方向打印对角矩阵吗?我想改变实际矩阵不仅仅是输出


我已经像这样更新了我的代码(如下所示),但仍然出现错误(如下所示)

#include<stdio.h>
    
void main()
{
     int n,m,a,b,c,d,i,j,s;
         int ar1[100][100];
      
     printf("Enter value of n to create a square matrix of order n (odd no.): ");
     scanf("%d",&n);

         printf("Enter m: (odd no.) ");
     scanf("%d",&m );
      
     
         if(n>m)
          { 
     for(i=0;i<n;i++)
     {
           for(j=0;j<n;j++)
           {
              
                       for(a=1;a<=(m-1)/2;a++){
                       if(ar1[i-a]==ar1[j]){
                       ar1[i][j] = 2;
                       printf("%d, ",ar1[i][j]);
                      }
                      }

                      for(a=(m+1)/2;a<=(n-1);a++){
                       if(ar1[i-a]==ar1[j]){
                       ar1[i][j] = -1;
                       printf("%d, ",ar1[i][j]);
                      }
                      }

                      if(ar1[i]==ar1[j]){
                       ar1[i][j] = 2;
                       printf("%d, ",ar1[i][j]);
                      }

                     for(a=1;a<=(m-1)/2;a++){
                       if(ar1[i+a]==ar1[j]){
                       ar1[i][j] = 2;
                       printf("%d, ",ar1[i][j]);

                      }
                      }

        for(a=(m+1)/2;a<=(n-1);a++){
                       if(ar1[i+a]==ar1[j]){
                       ar1[i][j] = -1;
                       printf("%d, ",ar1[i][j]);
                      }
                      }
s = flip (ar1[100][100], n);

             }
           printf("\n");
     }

void flip(int a[100][100], int n) {
 int t;
 for (i=0; i<n; i++) {
     for (j=0; j<n/2; j++) {
         t=a[i][j];
         a[i][j] = a[i][n-1-j];
         a[i][n-1-j] = t;
       }
   }
  }
    }
             
}

并且收到这样的错误

      /tmp/cc72v9hd.o: In function `main':
     faltu2.c:(.text+0x629): undefined reference to `flip'
     collect2: ld returned 1 exit status



    



       
4

2 回答 2

2

如果您只想更改输出,而不是实际矩阵,只需替换此外部循环:

for (i=0; i<n; i++) {
    ...

有了这个:

for (i=n-1; i>=0; i--) {
    ...

更新。如果您需要翻转矩阵,只需使用如下函数或代码:

 void flip(int a[][], int n) {
     int i,j,t;
     for (i=0; i<n; i++) {
         for (j=0; j<n/2; i++) {
             t=a[i][j];
             a[i][j] = a[i][n-1-j];
             a[i][n-1-j] = t;
         }
     }
 }
于 2013-07-08T09:33:09.627 回答
1

只需反转任何循环,即从n-1应该做的迭代.. 下面是我的版本,

#include<stdio.h>

void main()
{
    int n,m,a,b,c,d,i,j;
    int ar1[100][100];

    printf("Enter value of n to create a square matrix of order n (odd no.): ");
    scanf("%d",&n);

    printf(" matrix in diagonal direction is - \n");

    for(i=0;i<n;i++)
    {
            for(j=n-1;j>=0;j--)
            {
                    //if(ar1[j]==ar1[i]){       // any two garbage values can be equal
                    if(j==i)
                    {
                            ar1[j][i] = 2;
                            printf("%d, ",ar1[j][i]);
                    }
                    else
                    {
                            ar1[j][i] = -1;
                            printf("%d, ",ar1[j][i]);
                    }

            }
            printf("\n");
    }
}
于 2013-07-08T09:32:16.217 回答