首页 > 后端开发 > C++ > 正文

编写一个C程序,打印一个数组的下三角矩阵和上三角矩阵

WBOY
发布: 2023-08-31 15:17:06
转载
1934人浏览过

program description

write a program to print lower triangular matrix and upper triangular matrix of an array.

Triangular Matrix

A Triangular matrix is one that is either lower triangular or upper triangular.

Lower Triangular Matrix

A square matrix is called lower triangular if all the entries above the main diagonal are zero.

Upper Triangular Matrix

A square matrix is called upper triangular if all the entries below the main diagonal are zero.

A matrix of the form

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

is called a lower triangular matrix or left triangular matrix, and analogously a matrix of the form

$${displaystyle U={egin{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 & dots &&&ddots &u_{n-1,n}&&&&u_{n,n}end{bmatrix}}}$$

is called an upper triangular matrix or right triangular matrix. A lower or left triangular matrix is commonly denoted with the variable L, and an upper or right triangular matrix is commonly denoted with the variable U or R.

A matrix that is both upper and lower triangular is diagonal. Matrices that are similar to triangular matrices are called triangularisable.

Example − Upper Triangular Matrix

$${displaystyle {egin{bmatrix}{1}&{4}&{1}{0}&{6}&{4}{0}&{0}&{1}end{bmatrix}}}$$

Example − Lower Triangular Matrix

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

Algorithm

Examples − Different Dimensions of Matrices

编写一个C程序,打印一个数组的下三角矩阵和上三角矩阵

编写一个C程序,打印一个数组的下三角矩阵和上三角矩阵

For lower triangular matrix

Find the Index position of row and column.

If column position is greater than row position, then make that position 0.

For upper triangular matrix

Find the index position of row and column.

If column position is smaller than row position then make that position 0.

编写一个C程序,打印一个数组的下三角矩阵和上三角矩阵

Example

/* 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("</p><p>");
   printf("Please enter the number of columns for the matrix: ");
   scanf("%d", &cols);
   printf("</p><p>");
   printf("Please enter the elements for the Matrix: </p><p>");
   for(r = 0; r < rows; r++){
      for(c = 0;c < cols;c++){
         scanf("%d", &matrix[r][c]);
      }
   }
   printf("</p><p> The Lower Triangular Matrix is: ");
   for(r = 0; r < rows; r++){
      printf("</p><p>");
      for(c = 0; c < cols; c++){
      if(r >= c){
         printf("%d	 ", matrix[r][c]);
      }
      else{
         printf("0");
         printf("	");
      }
   }
   }
   printf("</p><p></p><p> The Upper Triangular Matrix is: ");
   for(r = 0; r < rows; r++){
      printf("</p><p>");
      for(c = 0; c < cols; c++){
         if(r > c){
            printf("0");
            printf("	");
         }
         else{
            printf("%d	 ", matrix[r][c]);

         }
      }
   }
   getch();
   return 0;
}
登录后复制

输出

编写一个C程序,打印一个数组的下三角矩阵和上三角矩阵

以上就是编写一个C程序,打印一个数组的下三角矩阵和上三角矩阵的详细内容,更多请关注php中文网其它相关文章!

全能打印神器
全能打印神器

全能打印神器是一款非常好用的打印软件,可以在电脑、手机、平板电脑等设备上使用。支持无线打印和云打印,操作非常简单,使用起来也非常方便,有需要的小伙伴快来保存下载体验吧!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号