Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

C에서 배열의 하부 삼각 및 상부 삼각 행렬을 인쇄하는 프로그램

<시간/>

프로그램 설명

배열의 하부 삼각 행렬과 상부 삼각 행렬을 출력하는 프로그램을 작성하십시오.

삼각 행렬

삼각 행렬은 하부 삼각 또는 상부 삼각 행렬입니다.

하삼각 행렬

정사각 행렬은 주대각선 위의 모든 항목이 0인 경우 하부 삼각행렬이라고 합니다.

상삼각 행렬

정사각 행렬은 주대각선 아래의 모든 항목이 0인 경우 상부 삼각행렬이라고 합니다.

형식의 행렬

$${\displaystyle L={\begin{bmatrix}\ell _{1,1}&&&&0\\\ell _{2,1}&\ell _{2,2}&&&\\\ell _{3, 1}&\ell _{3,2}&\ddots &&\\\vdots &\vdots &\ddots &\ddots &\\\ell _{n,1}&\ell _{n,2}&\ ldots &\ell _{n,n-1}&\ell _{n,n}\end{bmatrix}}}$$

하삼각행렬 또는 좌삼각행렬이라고 합니다. 유사하게 행렬

$${\displaystyle U={\begin{bmatrix}u_{1,1}&u_{1,2}&u_{1,3}&\ldots &u_{1,n}\\&u_{2,2}&u_{ 2,3}&\ldots &u_{2,n}\\&&\ddots &\ddots &\vdots \\&&&\ddots &u_{n-1,n}\\0&&&&u_{n,n}\end{bmatrix} }}$$

상삼각행렬 또는 우삼각행렬이라고 합니다. 하부 또는 좌삼각행렬은 일반적으로 변수 L로 표시되고, 상부 또는 우삼각행렬은 일반적으로 변수 U 또는 R로 표시됩니다.

상부 및 하부 삼각 행렬은 대각선입니다. 삼각 행렬과 유사한 행렬을 삼각화 가능이라고 합니다.

예 - 상부 삼각 행렬

$${\displaystyle {\begin{bmatrix}{1}&{4}&{1}\\{0}&{6}&{4}\\{0}&{0}&{1}\end {bmatrix}}}$$

예 - 하부 삼각 행렬

$${\displaystyle {\begin{bmatrix}{1}&{0}&{0}\\{2}&{8}&{0}\\{4}&{9}&{7}\end {bmatrix}}}$$

알고리즘

예 - 행렬의 다른 차원

C에서 배열의 하부 삼각 및 상부 삼각 행렬을 인쇄하는 프로그램

C에서 배열의 하부 삼각 및 상부 삼각 행렬을 인쇄하는 프로그램

하삼각행렬의 경우

행과 열의 인덱스 위치를 찾습니다.

열 위치가 행 위치보다 크면 해당 위치를 0으로 만듭니다.

상삼각 행렬의 경우

행과 열의 인덱스 위치를 찾습니다.

열 위치가 행 위치보다 작으면 해당 위치를 0으로 만듭니다.

C에서 배열의 하부 삼각 및 상부 삼각 행렬을 인쇄하는 프로그램

예시

/* Program to find Lower and Upper Triangle Matrix */
#include<stdio.h>
int main() {
   int rows, cols, r, c, matrix[10][10];
   clrscr(); /*Clears the Screen*/
   printf("Please enter the number of rows for the matrix: ");
   scanf("%d", &rows);
   printf("\n");
   printf("Please enter the number of columns for the matrix: ");
   scanf("%d", &cols);
   printf("\n");
   printf("Please enter the elements for the Matrix: \n");
   for(r = 0; r < rows; r++){
      for(c = 0;c < cols;c++){
         scanf("%d", &matrix[r][c]);
      }
   }
   printf("\n The Lower Triangular Matrix is: ");
   for(r = 0; r < rows; r++){
      printf("\n");
      for(c = 0; c < cols; c++){
      if(r >= c){
         printf("%d\t ", matrix[r][c]);
      }
      else{
         printf("0");
         printf("\t");
      }
   }
   }
   printf("\n\n The Upper Triangular Matrix is: ");
   for(r = 0; r < rows; r++){
      printf("\n");
      for(c = 0; c < cols; c++){
         if(r > c){
            printf("0");
            printf("\t");
         }
         else{
            printf("%d\t ", matrix[r][c]);

         }
      }
   }
   getch();
   return 0;
}

출력

C에서 배열의 하부 삼각 및 상부 삼각 행렬을 인쇄하는 프로그램