# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# define N 4
int main (void)
{
int a[N][N];
CreateArray(a);
PrintArray(a);
Convert(a);
printf("*************************\n");
PrintArray(a);
return 0;
}
int CreateArray(int a[N][N])
{
int i,j,temp;
srand((unsigned)time(NULL));
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
a[i][j]=rand()%100+1;
}
}
}
int Convert(int a[N][N])
{
int i,j,temp;
for (i=0;i<N;i++)
{
for (j=i+1;j<N;j++)
{
temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
}
int PrintArray(int a[N][N])
{
int i,j;
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
printf("%4i",a[i][j]);
}
printf("\n");
}
}
测试结果:
[root@localhost Gcc]# ./a.out
31 86 25 95
26 27 77 58
75 89 26 68
67 97 57 17
*************************
31 26 75 67
86 27 89 97
25 77 26 57
95 58 68 17