Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-09-02 09:17:05
forward
845 people have browsed it

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
Copy after login

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
Copy after login

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;
}
Copy after login

Output

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

1 0 0
4 5 0
7 8 9
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!