fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int arr[4][4]={{1,2,3,4},{5,5,7,8},{7,8,9,10},{8,9,10,11}};
  6.  
  7. int row= sizeof(arr)/sizeof(arr[0]);
  8. int col= sizeof(arr[0])/sizeof(arr[0][0]);
  9. //Print first row
  10. for(int j = 0; j < col; j++) {
  11. cout << arr[0][j] << " ";
  12. }
  13. // Print diagonal element
  14. int i = 1, j = col - 2; //its starts from row 1 and col 2
  15. while(i < row - 1 && j > 0) { //jab tak row 0 se row-1 nhi and col :col-2 to se 0 nhi
  16. cout << arr[i][j] << " ";
  17. i++; j--;
  18. }
  19. // Print last row
  20. for(int j = 0; j < col; j++) {
  21. cout << arr[row - 1][j] << " ";
  22. }
  23.  
  24. return 0;
  25. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
1 2 3 4 7 8 8 9 10 11