编写一个矩阵转置的函数,矩阵的行、列数在程序钟有用户输入。

如题所述

// zhuanzhi2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdio.h>
void fun(int *b,int M,int N)
{
int *array = new int[N*M];
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
*(array+i*M+j)=*(b+j*M+i);
printf("%d ",*(array+i*M+j));
}
printf("\n");
}
delete [] array;
}
int main(int argc, char* argv[])
{
int M,N,i,j;
printf("输入行数和列数\n");
scanf("%d %d",&M,&N);
int size = M*N;
int *array = new int[size];
printf("输入元素\n");
for(i=0;i<M;i++)
for(j=0;j<N;j++)
{
scanf("%d",array+i*M+j);
}
fun(array,M,N);
delete [] array;
return 0;
}
温馨提示:答案为网友推荐,仅供参考