二维数组的转置,镜像等操作

在主函数里对数组依次赋值,然后输出依次原数组,对其进行操作之后再次输出。

#include<stdio.h>
//转置
void T(int a[][4], int len1, int len2)
{
int t;
for (int i = 0; i < len1; ++i)
{
for (int j = 0; j < i; ++j)
{
t = a[j][i];
a[j][i] = a[i][j];
a[i][j] = t;
}
}
}
//垂直镜像
void HMirror(int a[][4], int len1, int len2)
{
int t;
for (int i = 0; i < len1; ++i)
{
for (int j = 0; j < len2 / 2; ++j)
{
t = a[i][j];
a[i][j] = a[i][len2 - 1 - j];
a[i][len2 - 1 - j] = t;
}
}

}
//水平镜像
void WMirror(int a[][4], int len1, int len2)
{
int t;
for (int i = 0; i < len1/2; ++i)
{
for (int j = 0; j < len2 ; ++j)
{
t = a[i][j];
a[i][j] = a[len1-1-i][j];
a[len1 - 1 - i][j] = t;
}
}
}
//逆90°先对原数组进行水平镜像,然后做转置操作。
void T1(int a[][4], int len1, int len2)
{
HMirror(a, len1, len2);
T(a, len1, len2);
}
//顺90°先对原数组转置,再进行水平镜像操作。
void T2(int a[][4], int len1, int len2)
{
T(a, len1, len2);
HMirror(a, len1, len2);
}
//180°对数组做两次顺(逆)时针转90°操作。
void T3(int a[][4], int len1, int len2)
{
T1(a, len1, len2);
T1(a, len1, len2);
}
int main()
{
int a[4][4];
int len1 = sizeof(a) / sizeof(a[0]);
int len2 = sizeof(a[0]) / sizeof(a[0][0]);

for (int i = 0; i < len1; ++i)
{
	for (int j = 0; j < len2; ++j)
	{
		a[i][j] = i*len2 + j + 1;//将1~16依次赋值给数组中每个元素
	}
}
for (int i = 0; i < len1; ++i)
{
	for (int j = 0; j < len2; ++j)
	{
		printf("%-3d", a[i][j]);
	}
	puts("");
}
puts("*************************");

// HMirror(a, len1, len2);
// T(a, len1, len2);
// WMirror(a, len1, len2);
// T3(a, len1, len2);
for (int i = 0; i < len1; ++i)
{
for (int j = 0; j < len2; ++j)
{
printf("%-3d", a[i][j]);
}
puts("");
}
return 0;
}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页