4.10 Print the primary and secondary diagonal of an array

Source: Internet
Author: User

1 //Print the main diagonal and sub-diagonal of a two-dimensional array matrix2#include <stdio.h>3 4 intMainvoid)5 {6     intarr[5][5] = {1,2,3,4,5,7                      6,7,8,9,Ten,8                       One, A, -, -, the,9                       -, -, -, +, -,Ten                       +, A, at, -, -}; One     inti,j; A #if0 -      for(i=0;i<5; i++) -     { the          for(j=0;j<5; j + +) -         { -printf"%d%d", i,j); -         } +Putchar (Ten); -     } +Putchar (Ten); A  at #endif -  - #if0//Method 1, the basic traversal of the data in memory storage in the order of filtering printing.  - //Print Main diagonal -      for(i=0;i<5; i++) -     { in          for(j=0;j<5; j + +) -         { to             if(i==j) +printf"%d%d", i,j); -         } thePutchar (Ten); *     } $Putchar (Ten);Panax Notoginseng //Print a secondary diagonal line -      for(i=0;i<5; i++) the     { +          for(j=0;j<5; j + +) A         { the             if(i+j==4) +printf"%d%d", i,j); -         } $Putchar (Ten); $     } -Putchar (Ten); -  the #endif - Wuyi #if0//Method 2 directly traverses only two diagonal lines and is efficient.  the //Print Main diagonal -      for(i =0;i<5; i++) Wu     { -printf"%d", Arr[i][i]); About     } $Putchar (Ten); - //Print a secondary diagonal line -      for(i =0;i<5; i++) -     { Aprintf"%d", arr[i][5-1-i]); +     } thePutchar (Ten); -  $ #endif the  the     return 0; the}

4.10 Print the primary and secondary diagonal of an array

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.