C ++ program to multiply lower triangular matrix with a upper triangular matrix.
Answers
/*This is a program to display the
upper triangular matrix and lower triangular matrix
for any given matrix. The method used here is
LU decomposition method. This program works for matrices
of order equal to or below 10x10*/
#include<stdio.h>
int main()
{
int size,row,column,ctr1,ctr2;
printf("ENTER THE SIZE OF THE MATRIX(size<10)\n");
scanf("%d",&size);
float matrix[10][10];
printf("ENTER THE MATRIX\n");
for(row=0;row<size;row++)
for(column=0;column<size;column++)
scanf("%f",&matrix[row][column]);
float lowertriangle[10][10];//matrices for lower and uppper triangle
float uppertriangle[10][10];
for(row=0;row<size;row++)
for(column=0;column<size;column++)
{
if(row>column)//initialise all elements of lower triangle in upper triangular matrix as 0
uppertriangle[row][column]=0.0;
if(row<column)//initialise all elements of upper triangle in lower triangular matix as 0
lowertriangle[row][column]=0.0;
if(row==column)//initialise all diagonal elements of lower triangular matrix as 1
lowertriangle[row][column]=1.0;
}
/*The algorithm used later is from the book Introduction to algorithms by Thomas Cormen.For a better understaning one can
read this text under chapter Matrix Operations. */
for(ctr1=0;ctr1<size;ctr1++)
{
uppertriangle[ctr1][ctr1]=matrix[ctr1][ctr1];
for(ctr2=ctr1+1;ctr2<size;ctr2++)
{
uppertriangle[ctr1][ctr2]=matrix[ctr1][ctr2];
lowertriangle[ctr2][ctr1]=matrix[ctr2][ctr1]/uppertriangle[ctr1][ctr1];
}
for(row=ctr1+1;row<size;row++)
for(column=ctr1+1;column<size;column++)
matrix[row][column]=matrix[row][column]-lowertriangle[row][ctr1]*uppertriangle[ctr1][column];
}
printf("UPPER TRIANGULAR MATRIX\n");
for(row=0;row<size;row++)
{//displays upper triangular matrix
for(column=0;column<size;column++)
printf("%.2f ",uppertriangle[row][column]);
printf("\n");
}
printf("LOWER TRIANGULAR MATRIX\n");
for(row=0;row<size;row++)
{//displays lower triangular matrix
for(column=0;column<size;column++)
printf("%.2f ",lowertriangle[row][column]);
printf("\n");
}
getch();
}
ENTER THE SIZE OF THE MATRIX
4
ENTER THE MATRIX
2.00 3.00 1.00 5.00
6.00 13.00 5.00 19.00
2.00 19.00 10.00 23.00
4.00 10.00 11.00 31.00
UPPER TRIANGULAR MATRIX
2.00 3.00 1.00 5.00
0.00 4.00 2.00 4.00
0.00 0.00 1.00 2.00
0.00 0.00 0.00 3.00
LOWER TRIANGULAR MATRIX
1.00 0.00 0.00 0.00
3.00 1.00 0.00 0.00
1.00 4.00 1.00 0.00
2.00 1.00 7.00 1.00
*/