2022级统计计算机双学位《程序设计原理与C语言》上机作业

1053. 矩阵转置

单点时限: 2.0 sec

内存限制: 256 MB

定义函数 transpose 转置一个 n 阶方阵。元素类型是 int。

//********** Specification of transpose **********
void transpose(void *a, int n)
/* PreCondition:
a is beginning address of a square matrix, n is the number of rows for the matrix, no more than 80.
PostCondition:
the matrix is transposed
*/

只需按要求写出函数定义,并使用给定的测试程序测试你所定义函数的正确性。

不要改动测试程序。

测试正确后,将测试程序和函数定义一起提交。

/***************************************************************/
/*                                                             */
/*  DON'T MODIFY main function ANYWAY!                         */
/*                                                             */
/***************************************************************/
#define N 10
/********** Specification of transpose **********/
void Transpose(void *a, int n)
/* PreCondition:
a is beginning address of a square matrix, n is the number of rows for the matrix, no more than 80.
PostCondition:
the matrix is transposed
*/
{

}
/***************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main()
{   int *a,n,i; scanf("%d",&n);
    a=(int*)malloc(n*n*sizeof(int));
    for (i=0;i<n*n;i++) scanf("%d",a+i);
    Transpose(a,n);
    for (i=0;i<n*n;i++)
    { printf("%d ",a[i]); if ((i+1)%n==0) printf("\n"); }
    free(a);
    return 0;
}