编写函数将一个M * N的矩阵转置成 N * M 的矩阵

任务描述

本关任务:编写函数将一个M * N的矩阵转置成一个N * M的矩阵,要求定义函数分别进行二维数组输入、输出和转置。

相关知识

M * N个数排成的MN列的数表称为MN列的矩阵,简称 M * N矩阵,在编程语言中可以当做二维数组来处理。

转置:把矩阵A的行换成同序数的列所得到的新矩阵B称为A的转置矩阵。

定义两个二维数组如下:

 
 
  1. int a[M][N],b[N][M];

如果3*5的矩阵A的值如下 : 55 8 74 66 23 32 12 1 36 58 94 45 56 88 31 那么矩阵A的转置矩阵B的值如下 : 55 32 94 8 12 45 74 1 56 66 36 88 23 58 31 MN有可能相等,也有可能不相等,针对二维数组不同的列数,定义不同的输出二维数组的函数:

 
 
  1. void Output1(int (*b)[M],int m,int n)
  2. {
  3. int i,j;
  4. for(i=0;i<m;i++)
  5. {
  6. for(j=0;j<n;j++)
  7. printf("%d\t",*(*(b+i)+j));
  8. printf("\n");
  9. }
  10. }
  11. void Output2(int (*b)[N],int m,int n)
  12. {
  13. int i,j;
  14. for(i=0;i<m;i++)
  15. {
  16. for(j=0;j<n;j++)
  17. printf("%d\t",*(*(b+i)+j));
  18. printf("\n");
  19. }
  20. }

编程要求

根据提示,在右侧编辑器 Begin-End 区间补充代码,编写函数输入一个3 * 5的矩阵,转置成一个5 * 3的矩阵,具体要求如下:

  • 函数 input() 实现二维数组的输入;
  • 函数 reverse() 实现二维数组的转置;
  • 函数 output2() 实现转置前二维数组的输出;
  • 函数 output1() 实现转置后二维数组的输出;
  • 函数 main() 定义变量并调用以上子函数。

测试说明

平台会对你编写的代码进行测试。

测试输入: 55 8 74 66 23 32 12 1 36 58 94 45 56 88 31

输入说明:输入15个整数。

预期输出: 55 8 74 66 23 32 12 1 36 58 94 45 56 88 31 55 32 94 8 12 45 74 1 56 66 36 88 23 58 31

输出说明: 首先输出35列的矩阵A; 再输出53列矩阵B,为矩阵A的转置矩阵,相邻两个整数之间用单个空格隔开。


开始你的任务吧,祝你成功!

数组传递的方法

#include <stdio.h>
#define M 3
#define N 5

void Input(int a[][N], int m, int n);
void reverse(int a[][N], int b[][M]);
void Output1(int a[][N], int m, int n);
void Output2(int a[][M], int m, int n);

int main()
{
    int RowC[M][N], RowCT[N][M], r = M, c = N;
    Input(RowC, r, c);
    reverse(RowC, RowCT);
    Output1(RowC, r, c);
    Output2(RowCT, c, r);

    return 0;
}

void Input(int a[][N], int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
}

void reverse(int a[][N], int b[][M])
{
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
        {
            b[j][i] = a[i][j];
        }
    }
}

void Output1(int a[][N], int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%d\t", a[i][j]);
        }
        printf("\n");
    }
}

void Output2(int a[][M], int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%d\t", a[i][j]);
        }
        printf("\n");
    }
}

指针传递 的方法 

#include <stdio.h>
#include <stdlib.h> // 添加头文件以使用动态内存分配函数

#define M 3
#define N 5

void Input(int (*a)[N], int m, int n);
void reverse(int (*a)[N], int (*b)[M], int m, int n);
void Output1(int (*a)[N], int m, int n);
void Output2(int (*a)[M], int m, int n);

int main()
{
    int (*RowC)[N], (*RowCT)[M], r = M, c = N;

    // 动态分配内存给RowC和RowCT
    RowC = malloc(r * sizeof(*RowC));
    RowCT = malloc(c * sizeof(*RowCT));

    // 分配内存后,检查内存分配是否成功
    if (RowC == NULL || RowCT == NULL)
    {
        printf("内存分配失败\n");
        return 1;
    }

    Input(RowC, r, c);
    reverse(RowC, RowCT, r, c);
    Output1(RowC, r, c);
    Output2(RowCT, c, r);

    // 释放动态分配的内存
    free(RowC);
    free(RowCT);

    return 0;
}

void Input(int (*a)[N], int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
}

void reverse(int (*a)[N], int (*b)[M], int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            b[j][i] = a[i][j];
        }
    }
}

void Output1(int (*a)[N], int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }
}

void Output2(int (*a)[M], int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }
}