C语言 二维数组转置之函数调用2

来源:互联网 发布:库里生涯数据统计 编辑:程序博客网 时间:2024/06/05 17:30

# 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                 

 

 

0 0
原创粉丝点击