二维数组+指针+函数实现矩阵转置

如题

#include <stdio.h>
#define ROW 3
#define COL 4//使用宏定义行和列
void Transpose(int *a, int *at, int row, int col);//转置 
void InputMatrix(int *s, int row, int col);//输入 
void PrintMatrix(int *s, int row, int col);//输出 


int main(void)
{
	int s[ROW][COL];				//s代表原矩阵
	int st[COL][ROW];				//st代表转置后的矩阵
	printf("Please enter matrix:\n");
		    
	//输入原矩阵,*s指向矩阵s的0行0列,是列指针
	/***************Begin**************/	
		//此处应有函数调用
	InputMatrix(*s,ROW,COL);

	/***************End***************/


	//对矩阵s进行转置,结果存放于st中
	/***************Begin**************/	
		//此处应有函数调用
	Transpose(*s, *st, ROW, COL);
		
	/***************End***************/


	printf("The transposed matrix is:\n");	  
	//输出转置矩阵,*st指向st的0行0列,是列指针
	/***************Begin**************/	
	//此处应有函数调用
	PrintMatrix(*st, COL,ROW);
	/***************End***************/

    return 0;
}
//函数功能: 对任意row行col列的矩阵a转置,转置后的矩阵为at
void Transpose(int *a, int *at, int row, int col)
{
	/***************Begin**************/
	int i,j;
    for(i=0;i<row;i++)
        for(j=0;j<col;j++)
        {
        	*(at+j*row+i)=*(a+i*col+j);
		}

	/***************End***************/
}
void InputMatrix(int *s, int row, int col)   //输入矩阵元素
{
	int i, j;
	for (i=0; i<row; i++)
	{
		for (j=0; j<col; j++)
		{
			scanf("%d", s+i*col+j);	//这里s+i*col+j等价于&s[i][j]
		}
	}
}
void PrintMatrix(int *s, int row, int col)  //输入矩阵元素
{
	int i, j;
	for (i=0; i<row; i++)
	{
		for (j=0; j<col; j++)
		{
			printf("%d\t",*(s+i*col+j));//这里*(s+i*col+j)等价于s[i][j]
		}
		printf(" \n");
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章