A C program to print the upper and lower triangle of matrix.

By | September 4, 2013

Let’s identify variables needed for this program.

In this program, we need to save matrices which consists Rows and Columns. To save this we need Two dimensional ARRAY.

Multi dimensional Array is a nothing different than any Array but the only difference is that it has more than one deminsion to it e.g. square has two dimension and cube has three dimension. The size of array is decided by us in number of square brackets [] depending upon the Dimension selected. If We select two dimension than we have to take two square brackets[][]. It has to be accessed with the help of index number ranging from 0 to n-1 and 0 to m-1. (e.g. num[0][0],num[0][1],num[2][2] so on.) 

First few variables will be the one which will save the values entered by the user and it will be array A[4][4] . Here we are taking4x4 matrices hence taking number 4 in both the square brackets. Upper and Lower triangle of the given matrix will be displayed later. other variables will be I and J which will be for FOR Loop so in all three variables.
The identified variables are A[3][3],I,J.

int A[4][4],I,J;

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

In this program there is a requirement of accessing the two deminsion array A[0][0],A[..][..],..A[4][4]. Here we have the index values ranging from zero to three (i.e. n-1) for ROWS and index values ranging from zero to three (i.e. n-1) for COLUMNS . Best Loop for such kind of condition is FOR Loop the value of loop will start with 0 to 3. Now here we need nested loop one loop inside the other. So that we can access all the Sixteen elements with it. We will need three nested loop in all six FOR loops, First nested loop for scanning required array values of a matrix. Next nested loops for printing resultant (upper and lower) array values as output.

This let’s you understand nested loop

for(I=0;I<4;I++)
    {
        for(J=0;J<4;J++)
        {
            scanf("%d",&A[I][J]);
        }
    }

Lower triangle of the matrix can be printed by just changing the column index J condition from J<4 to J<=I. If we remove equal to sign the diagonal values will not be printed. Here we are using \n and \t in the loop just for formatting the screen appearance correct alignment only.

printf("LOWER MATRIX C VALUES ARE :\n");
    for(I=0;I<4;I++)
    {
        printf("\n");
        for(J=0;J<=I;J++)
        {
            printf("%d\t",A[I][J]);
        }
        printf("\n");
    }

Upper triangle of the matrix can not be printed by just changing the column index J condition from J<4 to J>=I. which is only changing less than to greater than but putting the same condition inside in if condition (i.e. if(J>=I)) because the loop does not works with the condition for max times in the first loop. If we remove equal to sign the diagonal values will not be printed. Here we are using \n and \t in the loop just for formatting the screen appearance correct alignment only.

printf("UPPER MATRIX C VALUES ARE :\n");
    for(I=0;I<4;I++)
    {
        printf("\n");
        for(J=0;J<4;J++)
        {
             if(J>=I)
            printf("%d\t",A[I][J]);
             else
            printf("\t");
        }
        printf("\n");
    }

Finally print the Numbers of Two Dimension Resultant Array in the Matrix form by using Tab and New Line character in the nested Loop at the end of the program.

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int A[4][4],I,J;
  5.     clrscr();
  6.     printf("ENTER MATRIX A VALUES\n");
  7.     for(I=0;I<4;I++)
  8.     {
  9.         for(J=0;J<4;J++)
  10.         {
  11.             scanf("%d",&A[I][J]);
  12.         }
  13.     }
  14.     printf("LOWER MATRIX C VALUES ARE :\n");
  15.     for(I=0;I<4;I++)
  16.     {
  17.         printf("\n");
  18.         for(J=0;J<=I;J++)
  19.         {
  20.             printf("%d\t",A[I][J]);
  21.         }
  22.         printf("\n");
  23.     }
  24.     printf("UPPER MATRIX C VALUES ARE :\n");
  25.     for(I=0;I<4;I++)
  26.     {
  27.         printf("\n");
  28.         for(J=0;J<4;J++)
  29.         {
  30.              if(J>=I)
  31.             printf("%d\t",A[I][J]);
  32.              else
  33.             printf("\t");
  34.         }
  35.         printf("\n");
  36.     }
  37.     printf("\n");
  38.     getch();
  39. }

Note:- whenever you have large number of inputs to be entered, we can use space instead of enter button all the time. You can see have its done in Output screen.

SCREEN SHOTS:-

C_program_Matrix_Upper_Lower

C_program_Matrix_Upper_Lower_Output

Leave a Reply