Home > Backend Development > C++ > Print the diagonal pattern of a matrix

Print the diagonal pattern of a matrix

WBOY
Release: 2023-09-07 23:45:03
forward
1227 people have browsed it

Given a n*n two-dimensional array, the task is to find the anti-spiral arrangement of the given matrix

Input : arr[4][4]={1,2,3,4,
   5,6,7,8,
   9,10,11,12
   13,14,15,16}
Output : 1 6 11 16 4 7 10 13
Copy after login

Print the diagonal pattern of a matrix

Algorithm

START
Step 1 -> declare start variables as r=4, c=4, i and j
Step 2 -> initialize array as mat[r][c] with elements
Step 3 -> Loop For i=0 and i<r and i++
   Print mat[i][j]
Step 4 -> print </p><p>
Step 5 -> Loop For i=0 and i<r and i++
   Print mat[i][4-1-i]
End
STOP
Copy after login

Example# The Chinese translation of ## is:

Example

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
   int R=4,C=4,i,j;
   int mat[R][C] = { {1,2,3, 4}, {5,6,7,8},{9,10,11,12},{13,14,15,16}};
   for(i=0;i<R;i++) {
      cout<<mat[i][i]<<" ";
   }
   cout<<"</p><p>";
   for(i=0;i<R;i++) {
      cout<<mat[i][4-1-i]<<" ";
   }
}
Copy after login

Output

If we run the above program, it will generate the following output

1 6 11 16
4 7 10 13
Copy after login

The above is the detailed content of Print the diagonal pattern of a matrix. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template