[C Programming] Simple programming exercises-(4) Clockwise rotation of the square matrix and the transposition of the square matrix

table of Contents

1. Problem description

2. Problem solving

(1) The square matrix rotates clockwise (take the 3rd order square matrix as an example)

problem analysis:

programming:

(2) Square matrix transposition (take the 3rd order square matrix as an example)

problem analysis:

programming:


 

1. Problem description

problem:

1. Rotate a square matrix of order n by 90 degrees in a clockwise direction.

2. Transpose a square matrix of order n.

Examples:

1    2    3

4    5    6

7    8    9

The rear square matrix rotated 90 degrees clockwise is:

7   4    1

8    5    2

9    8    3

The square matrix after transposition is:

1    4   7

2    5   8

3   6   9

enter:

3

1    2    3

4    5    6

7    8    9

Output:

7   4    1

8    5    2

9    8    3

1    4   7

2    5   8

3   6   9


2. Problem solving

(1) The square matrix rotates clockwise (take the 3rd order square matrix as an example)

problem analysis:

For the solution of the numerical change problem in two-dimensional directions such as matrix, the most important thing is to find the law of position change.

① The position of the square matrix is ​​expressed as follows:

[0,0]     [0,1]       [0,2]

[1,0]     [1,1]       [1,2]

[2,0]     [2,1]       [2,2]

②Position change of the square matrix after rotating 90 degrees

[2,0]     [1,0]       [0,0]

[2,1]     [1,1]       [0,1]

[2,2]     [1,2]       [0,2]

According to the above position change, it can be seen that after the square matrix is ​​rotated 90 degrees, the column number of each row remains unchanged, and the row number of each row is successively decreased from left to right. Since the modification on the same square matrix is ​​done row by row from left to right, the modified value in the front will inevitably affect the value in the back. Therefore, an auxiliary matrix is ​​needed.

programming:

#include <stdio.h>

int main() {
	int n;
	int a[10][10];
	int b[10][10];
	//输入阶数
	scanf("%d", &n);
	//输入方阵的值
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			scanf("%d", &a[i][j]);
		}
	}
	
	//方阵顺时针变化
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			b[i][j] = a[n - j - 1][i];		
		}
	}

	//方阵打印
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			//printf("%-3d", b[i][j]);
			printf("%-3d", a[i][j]);
		}
		printf("\n");
	}
	
}

Run debugging results:

 


(2) Square matrix transposition (take the 3rd order square matrix as an example)

problem analysis:

① The position of the square matrix is ​​expressed as follows:

[0,0]     [0,1]       [0,2]

[1,0]     [1,1]       [1,2]

[2,0]     [2,1]       [2,2]

②The position change of the square matrix transposition

[0,0]     [1,0]       [0,0]

[0,1]     [1,1]       [2,1]

[0,2]     [1,2]       [2,2]

According to the above position change, it can be seen that after the square matrix is ​​transposed, the position of the number on the main diagonal (from the upper left corner to the lower right corner) does not change, and the position change of the numbers in other positions is the exchange of row and column numbers.

programming:

#include <stdio.h>

int main() {
	int n;
	int a[10][10];
	int b[10][10];
	//输入阶数
	scanf("%d", &n);
	//输入方阵的值
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			scanf("%d", &a[i][j]);
		}
	}

	//方阵转置
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			b[i][j] = a[j][i];
		}
	}

	//方阵打印
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			printf("%-3d", b[i][j]);
		}
		printf("\n");
	}

}

Run debugging results:

 

Guess you like

Origin blog.csdn.net/Jacky_Feng/article/details/109755947