Title Description: Enter an array (m*n dimension) that requires the elements of an array to be printed clockwise from the outside.
#include <iostream> #include <stdio.h>using namespace std;void printmatrixincircle (int **numbers,int rows, int columns,int start); void Printnumber (int number);
void printmatrixcircle (int **numbers,int rows,int columns) { //Verify that the input meets the requirements if (numbers==null| | rows<=0| | columns<=0) return; Each lap begins with an element in the upper left corner, Numbers[start][start] int start=0; The understanding of the loop condition while (rows>2*start&& (Columns>2*start)) { printmatrixincircle (numbers,rows, Columns,start); ++start;} }
void printmatrixincircle (int **numbers,int rows,int columns,int start) {//left like right print matrix for (int i=start;i<=columns-1-s tart;++i) {int number=numbers[start][i]; Printnumber (number); }//From top to bottom printing matrix//Prerequisites: Terminating line number > starting line number if (Start<rows-1-start) {for (int i=start+1;i<=rows-1-start;+ +i) {int Number=numbers[i][columns-1-start]; Printnumber (number); }}//Right-to-left printing matrix//Prerequisites: Terminating line number > starting line number, and terminating column number > starting column number if ((start<columns-start-1) && (start<rows-1 -start)) {for (int i=columns-2-start;i>=start;--i) {int number=numbers[rows-1-start][i]; Printnumber (number); }}//From the bottom up to print the matrix//precondition: At least three rows and two columns. Terminating line number-Starting line number = 2, and terminating column number > starting column number if ((start<rows-1-start-1) && (Start<columns-1-start)) {for (int i=rows- 1-start-1;i>=start+1;--i) {int Number=numbers[i][start]; Printnumber (number); }}}void PRintnumber (int number) {printf ("%d\t", number);}
void Test (int rows,int columns) { printf ("Test begin:%d columns,%d rows.\n", columns,rows); Non-qualifying input if (rows<1| | columns<1) return; int i; Consider each row of a two-dimensional array as a pointer array, //and specify rows as rows. int **numbers=new int*[rows]; for (I=0;i<rows;++i) { //For each row of a two-dimensional array, assign columns number. numbers[i]=new Int[columns]; for (int j=0;j<columns;++j) { numbers[i][j]=i*columns+j+1;//defines the value of the two-dimensional array } } Printmatrixcircle (numbers,rows,columns); printf ("\ n"); for (int i=0;i<rows;++i) delete[] (int*) numbers[i]; Delete[] numbers;}
int main () { Test (+); Test (4,5); return 0;}
Sword offer-Clockwise Print array