Analysis:Think of a matrix as several circles, and print a circle of the matrix with a loop.
# Include "stdafx. h "# include <iostream> using namespace std; void PrintMatrixIncircle (int ** nArr, int rows, int columns, int nStart) {int nEndX = columns-1-nStart; int nEndY = rows-1-nStart; // print a row from left to right (int I = nStart; I <= nEndX; I ++) {cout <nArr [nStart] [I] <"" ;}// print a column from top to bottom if (nEndY> nStart) {for (int j = nStart + 1; j <= nEndY; j ++) {cout <nArr [j] [nEndX] <"";}} // print a line from right to left if (nEndY> nStart & nEndX> nStart) {for (int t = nEndX-1; t> = nStart; t --) {cout <nArr [nEndY] [t] <";}}// print a column from bottom to top if (nEndY-1> nStart & nEndX> nStart) {for (int n = nEndY-1; n> = nStart + 1; n --) {cout <nArr [n] [nStart] <"";}}} // print the matrix clockwise. The number of rows is rows, and the number of columns is columnsvoid PrintMatrixClockWisely (int ** nArr, int rows, int columns) {if (nArr = NULL | rows <= 0 | columns <= 0) {return;} int nStart = 0; while (rows> (nStart * 2) & columns> (nStart * 2) {PrintMatrixIncircle (nArr, rows, columns, nStart); nStart ++ ;}} int _ tmain (int argc, _ TCHAR * argv []) {int nMatrix1 [4] [4] = {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}; int ** pp1 = new int * [4]; for (int I = 0; I <4; I ++) {pp1 [I] = nMatrix1 [I];} PrintMatrixClockWisely (pp1, 4, 4); cout <endl; int nMatrix2 [1] [4] = {1, 2, 3, 4}; int ** pp2 = new int * [1]; for (int I = 0; I <1; I ++) {pp2 [I] = nMatrix2 [I];} PrintMatrixClockWisely (pp2, 1, 4); cout <endl; int nMatrix3 [4] [1] = {1}, {2}, {3}, {4}; int ** pp3 = new int * [4]; for (int I = 0; I <4; I ++) {pp3 [I] = nMatrix3 [I];} PrintMatrixClockWisely (pp3, 4, 1 ); cout <endl; int nMatrix4 [2] [5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int ** pp4 = new int * [2]; for (int I = 0; I <2; I ++) {pp4 [I] = nMatrix4 [I];} PrintMatrixClockWisely (pp4, 2, 5 ); cout <endl; system ("pause"); return 0 ;}