Home  >  Article  >  Backend Development  >  In C program, print lower triangular matrix pattern from given array

In C program, print lower triangular matrix pattern from given array

WBOY
WBOYforward
2023-09-02 09:17:05830browse

Given an n x n matrix, the task is to print the matrix in the following triangular form.

The lower triangular matrix is ​​a matrix whose elements below the main diagonal include the main diagonal elements, and the remaining elements are zero.

We understand it through the following illustration:

In C program, print lower triangular matrix pattern from given array

#The green elements mentioned above are the elements below the main diagonal, and the red elements are above the main diagonal. elements, they are set to zero.

Example

Input: matrix[3][3] = {
   { 1, 2, 3 },
   { 4, 5, 6 },
   { 7, 8, 9 } }
Output:
   1 0 0
   4 5 0
   7 8 9

Algorithm

int lower_mat(int mat[n][m])
START
STEP 1: DECLARE I AND j
STEP 2 : LOOP FOR i = 0 AND i < n AND i++
   LOOP FOR j = 0 AND j < m AND j++
      IF i < j THEN,
         PRINT "0\t"
      ELSE
         PRINT mat[i][j]
      END IF
   END FOR
   PRINT newline
END FOR
STOP

Example

Chinese translation is:

Example

#include <stdio.h>
#define n 3
#define m 3
int lower_mat(int mat[n][m]){
   int i, j;
   for ( i = 0; i < n; i++){
      for ( j = 0; j < m; j++){
         if( i < j )
            printf("0\t");
         else
            printf("%d\t", mat[i][j]);
      }
      printf("</p><p>");
   }
}
int main(int argc, char const *argv[]){
   int mat[n][m] = {
      {1, 2, 3},
      {4, 5, 6},
      {7, 8, 9}
   };
   lower_mat(mat);
   return 0;
}

Output

If When we run the above program, the following output will be generated −

1 0 0
4 5 0
7 8 9

The above is the detailed content of In C program, print lower triangular matrix pattern from given array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete