Counting customers -- matrix inversion

Xiao Meng is currently working on a flip image application. As you may know, the image is actually composed of dots. Therefore, Xiaomeng wants to do a program that can flip the matrix first to solve the core part of his problem.

Input format

  the first input line includes integers m, N, t (0 < n, m < 200) m, N, t (0 < n, m < 200) separated by spaces, and the value of TT is 00 or 11. MM and NN represent the number of rows and columns of the matrix to be processed, respectively. When TT is 00, it means left and right flipping, and when TT is 11, it means up and down flipping.
MM line after  , each line includes NN integers separated by spaces, and then the data of each line of input matrix.

Output format

  the output includes MM rows and NN columns. Each number is separated by a space. There is a space at the end of each row, which represents the matrix after being flipped as required.

sample input
4 4 1
1 2 3 4
5 6 7 8
9 0 1 2
3 4 5 6
sample output
3 4 5 6 
9 0 1 2 
5 6 7 8 
1 2 3 4 
code implementation
#include <stdio.h>
void Swap(int *pa, int *pb)
{
    int tmp = *pa;
    *pa = *pb;
    *pb = tmp;
}

void SwapUpDown(int arr[][200], int m, int n)
{
    int i = 0, j = 0;
	//Flip up and down -- > exchange line by line
		for (i = 0; i < (m / 2); ++i)
		{//i representative bank
			for (j = 0; j < n; ++j)
			{//j representative column
                Swap(&arr[i][j], &arr[m - 1 - i][j]);
			}
		}
}
void SwapLeftRight(int arr[][200], int m, int n)
{
        int i = 0, j = 0;
    //Flip left and right -- > swap column by column
		for (i = 0; i < (n / 2); ++i)
		{//i representative column
			for (j = 0; j < m; ++j)
			{//j representative bank
                Swap(&arr[j][i], &arr[j][n - 1 - i]);
			}
		}
}
int main()
{
	int m, n, t;	//m stands for row, n stands for column; when t is 1, it stands for flipping up and down; when t is 0, it stands for flipping left and right
	scanf("%d%d%d", &m, &n, &t);
	int arr[200][200] = { 0 };
	int i = 0, j = 0;
	//1. Elements in input matrix
	for (i = 0; i < m; ++i)
	{
		for (j = 0; j < n; ++j)
		{
			scanf("%d", &arr[i][j]);
		}
	}
	//2. Realize flipping
	if (t == 0)
	{
		//Flip left and right
        SwapLeftRight(arr, m, n);
	}
	else if (t == 1)
	{
		//Flip left and right
        SwapUpDown(arr, m, n);
	}
	//3. Output the flipped matrix
	for (i = 0; i < m; ++i)
	{
		for (j = 0; j < n; ++j)
		{
			printf("%d ", arr[i][j]);
		}
		printf("\n");
	}

	return 0;
}

Posted by coho75 on Sun, 22 Dec 2019 13:28:47 -0800