C Program to Find Diagonal Upper and Lower Triangle of Matrix

Get Certified in C Programming for Free and Take Your Skills to the Next Level

Matrices are fundamental mathematical structures used in various fields, including computer science and data analysis. They offer a convenient way to represent and manipulate data in a tabular format. One common task involving matrices is finding the diagonal upper and diagonal lower triangles. In this article, we will discuss a C Program to Find Diagonal Upper and Lower Triangle of Matrix. Let’s start!!!

What is Diagonal Upper and Lower Triangle of Matrix

To begin, let’s understand what the diagonal upper and diagonal lower triangles of a matrix are. Consider a square matrix, where the number of rows is equal to the number of columns. The diagonal elements of a matrix are those elements that lie on the principal diagonal, which runs from the top left to the bottom right of the matrix. The diagonal upper triangle consists of all the elements above the principal diagonal, whereas the diagonal lower triangle contains all the elements below the principal diagonal.

Implementation of C Program to find Diagonal Upper and Lower Triangle of Matrix

#include <stdio.h>
#define SIZE 100
void findDiagonalTriangles (int matrix[][SIZE], int n) {
int i, j;
// Diagonal Upper Triangle
printf("Diagonal Upper Triangle:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (j > i) {
}}}
printf("%d", matrix[i][j]);
printf("\n");
// Diagonal Lower Triangle
printf("Diagonal Lower Triangle:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) { if (i > j) {
printf("%d", matrix[i][j]);
}}
printf("\n");
}
int main() {
int matrix[SIZE][SIZE];
int n, i, j;
printf("Enter the size of the matrix: ");
scanf("%d", &n);
printf("Enter the elements of the matrix:\n"); for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
findDiagonal Triangles (matrix, n);
return 0;

Let’s go through the program step by step. We start by including the necessary header file, `stdio.h`, which provides input/output functionality. We also define a constant `SIZE` to represent the maximum size of the matrix. Feel free to modify this value as per your requirements.

Next, we define the `findDiagonalTriangles` function. This function takes two parameters: the `matrix` itself, and `n`, which represents the size of the matrix. Inside the function, we iterate over the matrix using two nested loops to access each element. For the diagonal upper triangle, we check if the column index is greater than the row index, and if so, we print the corresponding element. Similarly, for the diagonal lower triangle, we check if the row index is greater than the column index, and print the element accordingly.

In the `main` function, we first declare the `matrix` array and variables `n`, `i`, and `j` for matrix size and loop counters, respectively. We prompt the user to enter the size of the matrix and read the value using `scanf`. Then, we ask the user to enter the elements of the matrix, reading them into the `matrix` array.

Finally, we call the `findDiagonalTriangles` function, passing the `matrix` and `n` as arguments. The function prints the diagonal upper and diagonal lower triangles of the matrix.

To run this program, compile it using a C compiler, and execute the resulting binary. Enter the size of the matrix and provide the matrix elements as input. The program will display the diagonal upper and diagonal lower triangles accordingly.

Output

Enter the size of the matrix: 3
Enter the elements of the matrix:
2 3 4 5 6 7 8 9 2
Diagonal Upper Triangle:
3 4
7Diagonal Lower Triangle:
5
8 9

Method 2: Using Pointers to Find Diagonal Upper and Diagonal Lower Triangle of Matrix

#include <stdio.h>
#define SIZE 100
void findDiagonalTriangles(int *matrix, int n) {
int i, j;
// Diagonal Upper Triangle
printf("Diagonal Upper Triangle:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (j > i) {
}
printf("%d ", *((matrix + i * SIZE) + j));
printf("\n");
}
}
// Diagonal Lower Triangle
printf("Diagonal Lower Triangle:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
}
if (i > j) {
}
printf("%d", *((matrix + i * SIZE) + j));
printf("\n");
}
}
int main() {
int
matrix[SIZE][SIZE];
int n, i, j;
printf("Enter the size of the matrix: ");
scanf("%d", &n);
printf("Enter the elements of the matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
}
findDiagonalTriangles(&matrix[0][0], n);
return 0;

In this alternative implementation, we use pointers to access the elements of the matrix instead of directly indexing them. We pass a pointer to the first element of the matrix (`&matrix[0][0]`) to the `findDiagonalTriangles` function. Within the function, we use pointer arithmetic to access the matrix elements. The rest of the code remains the same.

Method 3: Using a One-Dimensional Array

#include <stdio.h>
#define SIZE 100
void findDiagonalTriangles(int matrix[], int n) {
int i, j;
// Diagonal Upper Triangle
printf("Diagonal Upper Triangle:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (j > i) {
printf("%d", matrix[i * SIZE + j]);
}
}
printf("\n");
}
// Diagonal Lower Triangle
printf("Diagonal Lower Triangle:\n"); for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i > j) {
}
}
printf("%d", matrix[i * SIZE + j]);
printf("\n");
}
int main() {
int matrix[SIZE * SIZE]; int n, i, j;
printf("Enter the size of the matrix: "); scanf("%d", &n);
printf("Enter the elements of the matrix:\n");
for (i =
0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i * SIZE + j]);
}
}
}
findDiagonal Triangles(matrix, n);
return 0;

In this alternative implementation, we use a one-dimensional array (`matrix[SIZE * SIZE]`) to store the matrix elements. The elements are accessed using the formula `matrix[i * SIZE + j]`, where `i` and `j` represent the row and column indices, respectively. The rest of the code remains the same.

These alternative implementations provide different approaches to solving the problem while achieving the same results. You can choose the one that suits your preferences or fits the requirements of your project.

Conclusion

In conclusion, we have discussed a C program that finds the diagonal upper and diagonal lower triangles of a given matrix. This program can be helpful in various applications involving matrix analysis and manipulation. Understanding these triangles and being able to extract their elements is essential in many algorithms and computations. Feel free to modify and extend this program based on your specific requirements or use it as a building block for more complex matrix operations.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *