Computer Science‎ > ‎

C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix


C program to display the upper triangular matrix and lower triangular matrix for any given matrix



/*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();
}
/*A sample run of the program is found to give following result
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
*/

Other Programs which you might find useful:


C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers