由于行、列不确定,所以要动态处理。举例代码如下:
//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
#include "stdlib.h"
int main(void){
int **a,**b,m,n,i,j;
while(1){//保证输入的行、列数为正数
printf("Please enter m & n(int m>0,n>0)...\n");
if(scanf("%d%d",&m,&n),m>0 && n>0)
break;
printf("Error, redo: ");
}
a=(int **)malloc((j=sizeof(int))*m);
b=(int **)malloc(j*n);
if(a==NULL || b==NULL){
printf("Application memory failure...\n");
exit(0);
}
for(i=0;iif((a[i]=(int *)malloc(j*n))==NULL){
printf("Failed to create the array...\n");
exit(0);
}
for(i=0;iif((b[i]=(int *)malloc(j*m))==NULL){
printf("Failed to create the array...\n");
exit(0);
}
for(i=0;ifor(j=0;j printf("\n");
for(i=0;ifor(j=0;j printf("%d ",b[i][j]=a[j][i]);//a的行、列互换后存入b
printf("\n");
}
for(i=0;ifree(a);
for(i=0;ifree(b);
return 0;
}
你的意思是a[i][j]的数组就保存a[j][i]? 那头一个数应该不会变啊
只需要换二维数组下标即可,不需要借用中间数组b[][]
#include "stdio.h"
void main()
{
int **a;
int row,col;
int i,j;
printf("Please input row and col:\n");
scanf("%d%d",&row,&col);
printf("Please input data:\n");
a=(int**)malloc(row*sizeof(int));
for(i=0;i
for(i=0;i
printf("Result:\n");
for(i=0;i
for(j=0;j
printf("\n");
}
}
如果对你有所帮助,请记得采纳最佳答案,谢谢!
#include
main()
{
int a[20][20];
int m,n,i,j;
scanf(
void zhuanhuan(int a[MAX1][],int b[MAX2][],int m,int n)
{
int i,j;
for(i=0;i
for(i=0;i
if(i