Print the matrix clockwise (c language code)

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h>void printmatrix (int **matrix,int columns,int rows,int start) {int x= Columns-start-1;int y=rows-start-1;int i;for (i=start;i<=x;i++)//First print a line of printf ("%d", matrix[start][i]); if (start <y)//from top down {for (i=start+1;i<=y;i++) printf ("%d", matrix[i][x]);} if (X>start&&y>start)//right-to-left printing {for (i=x-1;i>=start;i--) printf ("%d", Matrix[y][i]);} if (x>start&&y>start+1)//from bottom to top print {for (i=y-1;i>=start+1;i--) printf ("%d", Matrix[i][start]);}} void Matrixclock (int **matrix,int columns,int rows) {if (matrix==null| | columns<=0| | rows<=0) Return;int Start=0;while (columns>start*2&&rows>start*2) {PrintMatrix (Matrix,columns, Rows,start); start++;}} int main () {int n,m;while (scanf ("%d%d", &n,&m)!=null) {int **numbers;int i,j;numbers= (int * *) malloc (sizeof (int *) *n),//n rowsfor (i=0;i<n;i++) numbers[i]= (int *) malloc (sizeof (int) *m),//m columnsfor (i=0;i<n;i++) for (j=0;j <m;j++) scanf ("%d", &numbers[i][j]); Matrixclock (Numbers,M,n);} return 0;}

Print the matrix clockwise (c language code)

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.