Print the matrix clockwise--20

Source: Internet
Author: User

Enter a matrix that prints each number in a clockwise order from outward. For example, enter the following matrix:

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

The word 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10 is printed in turn.


In fact, printing clockwise, nothing but first print out the first line of the Matrix, then the last column, then the last row, and finally the first column, each time the printing control of the condition is the matrix row and column boundaries, Then after printing a lap only need to change the print range is the first and last row and column can be printed inside the matrix;


The program is as follows:

#include  <iostream> #include  <assert.h>using namespace std;     void clockwiseprintarr (Int arr[][5], size_t row, size_t col) {     assert (Arr && row && col);//Condition judgment     int   (*TMP) [5] = arr;        //first start of behavior 0, column is also 0, print the number of terminated behavior parameter rows, The column is also the number of formal parameter columns     int start_row = 0;    int start_col  = 0;    int end_row = row;    int end_ col = col;    //judgment condition when the starting boundary is smaller than the terminating boundary     while (start_row  < end_row)  &&  (start_col < end_col)     {                //Print matrix first row         &Nbsp;for (int i = start_col; i < end_col; ++i)          {            cout<< tmp[start_row][i]<< " ";        }             //the last column of the print Matrix         for ( Int i = start_row+1; i < end_row; ++i)          {            cout<<tmp[i][ end_col-1]<< " ";        }                 //print matrix last line          for (int i = end_col-2; i >= start_col; --i)        &nbsP; {            cout<<tmp[end_row-1][i] << " ";        }                  //Print matrix first column          for (int i = end_row-2; i > start_row; --i)          {            cout <<tmp[i][start_col]<< " ";        }                 //reduce the printed border by one turn          ++start_row;        ++start_col;         --end_row;        --end_col;     }     cout<<endl;} Int main () {    int arr[5][5] = {{0,  1,  2,   3,  4},              {5,   6,  7,  8,  9},               {10, 11, 12, 13, 14},               {15, 16, 17, 18, 19},               {20, 21, 22, 23,  24}};    clockwiseprintarr (arr, sizeof (arr)/sizeof (arr[0]),  sizeof (arr[0])/ sizeof (arr[0][0]);     return 0;}


To run the program:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/80/27/wKioL1c5qUDQtZSwAAAPTI2j0ig833.png "title=" Colckwiseprintarr.png "alt=" Wkiol1c5qudqtzswaaapti2j0ig833.png "/>



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1774031

Print the matrix clockwise--20

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.