top of page

Printing Upper Right and Lower Left Triangles of a Matrix in C

Introduction


Matrices are essential in programming for representing data in rows and columns. Today, we will learn how to extract specific parts of a matrix: the upper right triangle and the lower left triangle. This is useful for simplifying data or focusing on specific sections of a matrix. Let's break this down step-by-step using a C program.


What is a Matrix?


A matrix is a collection of numbers arranged in rows and columns. For example, a 4x4 matrix looks like this:


1  2  3  4
5  6  7  8
9 10 11 12
13 14 15 16

Here, each element has a specific position (identified by row and column indices).


Upper Right and Lower Left Triangles of a Matrix


Matrices can be divided into different parts. Today, we will focus on two important sections:

  • Upper Right Triangle: This contains the elements above the main diagonal (including the diagonal).

  • Lower Left Triangle: This contains the elements below the main diagonal (including the diagonal).

For our 4x4 matrix above:

  • The upper right triangle includes elements like 1, 2, 3, 4, 6, 7, 8, etc.

  • The lower left triangle includes elements like 1, 5, 6, 9, 10, 11, etc.


Writing a C Program


Let's write a simple C program that will help us print these triangles.


Code to Print the Upper Right Triangle


The upper right triangle contains all the elements where the column index (j) is greater than or equal to the row index (i). Here's the code to do that :


#include <stdio.h>

#define SIZE 4 // Size of the matrix

void printUpperRightTriangle(int matrix[SIZE][SIZE], int size) {

    printf("Upper Right Triangle:\n");

    for (int i = 0; i < size; i++) {

        for (int j = 0; j < size; j++) {

            if (j >= i) {

                printf("%d ", matrix[i][j]);

            } else {

                printf("  "); // Print spaces to align output

            }

        }

        printf("\n");

    }

}

Code to Print the Lower Left Triangle


The lower left triangle contains all the elements where the column index (j) is less than or equal to the row index (i). Here's the code to do that :


void printLowerLeftTriangle(int matrix[SIZE][SIZE], int size) {

    printf("Lower Left Triangle:\n");

    for (int i = 0; i < size; i++) {

        for (int j = 0; j < size; j++) {

            if (j <= i) {

                printf("%d ", matrix[i][j]);

            } else {

                printf("  "); // Print spaces to align output

            }

        }

        printf("\n");

    }

}

Full C Program


Combining everything, here's the complete C program :


#include <stdio.h>

#define SIZE 4 // Define the size of the matrix

void printUpperRightTriangle(int matrix[SIZE][SIZE], int size) {

    printf("Upper Right Triangle:\n");

    for (int i = 0; i < size; i++) {

        for (int j = 0; j < size; j++) {

            if (j >= i) {

                printf("%d ", matrix[i][j]);

            } else {

                printf("  "); // Print spaces to align output

            }

        }

        printf("\n");

    }

}

void printLowerLeftTriangle(int matrix[SIZE][SIZE], int size) {

    printf("Lower Left Triangle:\n");

    for (int i = 0; i < size; i++) {

        for (int j = 0; j < size; j++) {

            if (j <= i) {

                printf("%d ", matrix[i][j]);

            } else {

                printf("  "); // Print spaces to align output

            }

        }

        printf("\n");

    }

}

int main() {

    int matrix[SIZE][SIZE] = {

        {1, 2, 3, 4},

        {5, 6, 7, 8},

        {9, 10, 11, 12},

        {13, 14, 15, 16}

    };

    printUpperRightTriangle(matrix, SIZE);

    printf("\n");

    printLowerLeftTriangle(matrix, SIZE);

    return 0;

}

Understanding the Output


After running the program, you will see:


Upper Right Triangle :


1 2 3 4 

  6 7 8 

   11 12 

      16

Lower Left Triangle :


1 

5 6 

9 10 11 

13 14 15 16

In both triangles, we align the values using spaces so that they look like a triangle. This makes it visually clear and easy to understand.


Conclusion


Printing specific parts of a matrix can help in focusing on important elements and simplifying calculations. In this blog, we explored how to print the upper right and lower left triangles of a matrix using C. With the help of loops and conditions, extracting parts of a matrix becomes an easy task.

Related Posts

See All

C - Pointers

Pointers in C . Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation,...

Commentaires


Les commentaires ont été désactivés.
bottom of page