简体   繁体   English

一个用于转置矩阵的程序,但在这个打印结果的程序中它有一个错误

[英]a program for transposing a matrix but in this program in printing result it has a bug

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

    int main(){
        int m,n,i,j,t;
        printf("in order enter line and column of matrix :");
        scanf("%d%d",&n,&m);
        int a[n][m],b[m][n];
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                printf("a[%d][%d]= ",i+1,j+1);
                scanf("%d",&a[i][j]);
              }
          }
        printf("first matrix:\n");
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                printf("%d    ",a[i][j]);
              }
              printf("\n\n");
          }
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                b[j][i]=a[i][j];
            }
        }
        printf("transposed matrix:\n");
        for(i=0;i<m;i++){
            for(j=0;j<n;j++){
                printf("%d    ",b[j][i]);
              }
              printf("\n\n");
        }

      }

a program for transposing a matrix but in this program in printing result it has a bug, for example, this program for a Square matrix print first matrix一个用于转置矩阵的程序,但在这个程序中打印结果它有一个错误,例如,这个用于方形矩阵的程序打印第一个矩阵

You've interchanged the row and column indices.您已经交换了行索引和列索引。 Printing b[i][j] works fine.打印b[i][j]工作正常。 Even with non square matrices.即使是非方阵。

printf("transposed matrix:\n");
for(i=0;i<m;i++)
{
    for(j=0;j<n;j++)
    {
        printf("%d    ",b[i][j]);
    }
    printf("\n\n");
 }

Here is an example:这是一个例子:

a[1][1]= 1
a[1][2]= 2
a[2][1]= 3
a[2][2]= 4
a[3][1]= 5
a[3][2]= 6
first matrix:
1    2    

3    4    

5    6    

transposed matrix:
1    3    5    

2    4    6 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM