Home > Backend Development > C++ > In C program, print numbers in matrix diagonal pattern

In C program, print numbers in matrix diagonal pattern

王林
Release: 2023-09-14 18:25:06
forward
653 people have browsed it

The task is to print an n x n matrix in diagonal pattern.

If n is 3, then print a diagonal pattern matrix as follows:

In C program, print numbers in matrix diagonal pattern

So the output will be:

In C program, print numbers in matrix diagonal pattern

Example

Input: 3
Output:
   1 2 4
   3 5 7
   6 8 9
Input: 4
Output:
   1 2 4  7
   3 5 8 11
   6 9 12 14
   10 13 15 16
Copy after login

The question suggests that we are given a number n and generate a matrix of n x n, then we have to traverse the matrix in a diagonal pattern and store the values ​​in a in a separate matrix.

But this will increase the complexity of our code, so we will −

  • Create a size of N X N Matrix which will store the pattern before printing.

  • Store elements in the upper triangle of the pattern. Observe that as you move down the diagonal, the row index increases by 1 and the column index decreases by 1.

  • After completing the upper triangle, store the elements of the lower triangle in a similar way to the upper triangle, i.e. as you move down the diagonal, the row index increases by 1 and the column index decreases 1. The Chinese translation of

Algorithm

int printdiagonal(int n)
START
STEP 1: DECLARE int mat[n][n], i, j, k, d=1, m
STEP 2: LOOP FOR i = 0 AND i < n AND i++
   ASSIGN j AS i AND k AS 0
   LOOP FOR j = I AND j >= 0 AND j--
      ASSIGN mat[k][j] AS d
      INCREMENT d AND k BY 1
   END LOOP
END LOOP
STEP 3: LOOP FOR k = 1 AND k < n AND k++
   ASSIGN i AND m EQUALS TO k
   LOOP FOR j = n-1 AND j >= m AND j--
      ASSIGN mat[i][j] AS d;
      INCREMENT d AND i WITH 1
   END FOR
END FOR
STEP 4: LOOP FOR i = 0 AND i < n AND i++
   LOOP FOR j = 0 AND j < n AND j++
      PRINT mat[i][j]
   END FOR
   PRINT NEWLINE
END FOR
STOP
Copy after login

Example

is:

Example

#include <stdio.h>
int printdiagonal(int n){
   int mat[n][n], i, j, k, d=1, m;
   for ( i = 0; i < n; i++){
      j = i;
      k = 0;
      for ( j = i; j >= 0; j--){
         mat[k][j] = d;
         d++;
         k++;
      }
   }
   for ( k = 1; k < n; k++){
      i = m = k;
      for ( j = n-1; j >= m; j--){
         mat[i][j] = d;
         d++;
         i++;
      }
   }
   for ( i = 0; i < n; i++){
      for(j = 0; j < n; j++){
         printf("%d ", mat[i][j] );
      }
      printf("</p><p>");
   }
}
int main(int argc, char const *argv[]){
   int n = 3;
   printdiagonal(n);
   return 0;
}
Copy after login

Output

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

1 2 4
3 5 7
6 8 9
Copy after login

The above is the detailed content of In C program, print numbers in matrix diagonal pattern. 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