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

该博客展示了如何使用C语言编写程序,实现二维数组的转置操作。首先定义了一个4x4的二维数组并填充随机数,然后通过调用Convert函数完成转置,最后打印转置后的数组。代码中包括CreateArray创建数组,PrintArray打印数组以及Convert转置数组的函数。

# 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                 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值