二维数组或矩阵旋转180度

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

typedef  unsigned char u8;
typedef  unsigned short u16;

#define ROW 4
#define COL 3

u16 data[ROW][COL] = {
	
	1,2,3,
	4,5,6,
	7,8,9,
	10,11,12

};


void rotate_180_degree(u16 weight,u16 height,u16 *pframe_buffer)
{
	u16 temp;
	u16 i,j;
	u16 times = height/2;
	
	u16 *pfront;
	u16 *back;
	for(i=0;i<times;i++)
	{
		pfront = pframe_buffer+i*weight;
		back = pframe_buffer + (height-1-i)*weight;
		for(j=0;j<weight;j++)
		{
			temp = pfront[j];
			pfront[j] = back[weight-1-j];
			back[weight-1-j] = temp;
		}
	}
	
	if(height&0x0001)
	{
		pfront = pframe_buffer+times*weight;
		for(j=0;j<weight/2;j++)
		{
			temp = pfront[j];
			pfront[j] = pfront[weight-1-j];
			pfront[weight-1-j] = temp;
		}			
	}	
}

void printf_data()
{
	u16 i,j;
	for(i=0;i<ROW;i++)
	{
		for(j=0;j<COL;j++)
		{
			printf("%02d ",data[i][j]);
		}
		printf("\n");	
	}
}


int main(int argc, char *argv[]) {
	printf_data();
	rotate_180_degree(COL,ROW,&data[0][0]);
	printf("\n\n");
	printf_data();
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值