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