C语言,利用指针将数组的行与列互换 一定要用C语言,谢谢,在线等
发布网友
发布时间:2022-05-18 11:38
我来回答
共1个回答
热心网友
时间:2023-10-15 22:33
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define M 5
#define N 5
void main()
{
void create(int a[][N]);
void swap(int **a);
void show(int (*p)[N]);
int a[M][N], *p[M], i, j;
create(a);
for ( i = 0; i < M; i++)
{
p[i] = a[i];
for ( j = 0; j < N; j++)
printf ("%3d", a[i][j]);
printf ("\n");
}
printf ("\n");
swap(p);
printf ("行与列互换后:\n");
show(a);
}
void create(int a[][N])
{
int i, j;
srand ((unsigned) time (NULL));
for ( i = 0; i < M; i++)
for ( j = 0; j < N; j++)
a[i][j] = rand () % 100;
}
void swap(int **a)
{
int i, j, t;
for (i = 0; i < M; i++)
for (j = 0; j < i; j++)
{
t = a[i][j];
a[i][j] = a[j][i];
a[j][i] = t;
}
}
void show(int (*p)[N])
{
int i, j;
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
printf ("%3d", p[i][j]);
printf ("\n");
}
}