Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In this blog, we will discuss the solution of the problem of finding the matrix formed by interchanging its diagonals. We will be covering the C program to interchange diagonals of a matrix along with their time and space complexities.
Before discussing the C Program to Interchange Diagonals of a Matrix, let’s first discuss what is meant by a matrix and its diagonal.
Matrix: A matrix is an array or table of numbers arranged in rows and columns, usually denoted by a capital letter.
Diagonal of Matrix: The diagonal of a matrix represents the elements present diagonally in the matrix. The elements from the top left corner to the bottom right corner represent the primary diagonal, and the elements from the top right corner to the bottom left corner represent the secondary diagonal.
For Example:
We have taken a 2x2 matrix, in which the blue box represents the primary diagonal and the pink box represents the secondary matrix. Interchange the blue box(primary diagonal) and pink box(secondary diagonal) with each other, and the resultant matrix is the Green matrix.
We need to write a C program to interchange Diagonals of a Matrix by reading the size and elements of the matrix from the user and interchange the elements of the primary diagonal with the secondary diagonal. Also, check if the matrix is a square matrix or not. If it is not a square matrix, give a warning message and again execute from starting.
Algorithm
Step 1: Declare two integer variables, ‘row’ and ‘col’
Step 2: Read the size of the matrix in ‘row’ and ‘col’
Step 3: Check if ‘row’ != ‘col’. If this condition is true, display a warning message saying
“Please Enter a Square Matrix !!!” and move to step 2.
Step 4: Declare a two-dimensional matrix ‘arr[row][col]’ and a integer variable ‘temp’
Step 5: Run a nested for loop from ‘i’ = ‘0’ to ‘row’ and ‘j’ = ‘0’ to ‘col’
Step 6: Read the array elements from the user using the ‘scanf’ function.
Step 7: Run a for loop from ‘i’ = ‘0’ to ‘row’
Step 8: Swap the elements present on the diagonals using following code
temp = arr[i][i];
arr[i][i] = arr[i][row - i - 1];
arr[i][row - i - 1] = temp;
Step 9: Run a nested for loop from ‘i’ = ‘0’ to ‘row’ and ‘j’ = ‘0’ to ‘col’
Step 10: Display the matrix elements as ‘arr [i] [j]’
Code
/* C Program to Interchange Diagonals of a Matrix */
#include<stdio.h>
int main()
{
int row, col;
label1:
printf("\n Enter Number of rows and columns : ");
scanf("%d %d", &row,&col);
// Check if it is a square matrix or not
if(row != col)
{
printf("\n Please Enter a Square Matrix !!!" );
goto label1;
}
int arr[row][col], temp;
printf("\n Enter the Matrix Elements \n");
// Nested loop to input array elements from the user
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
scanf("%d", &arr[i][j]);
}
}
// Code to swap the diagonals
for(int i=0;i<row;i++)
{
temp = arr[i][i];
arr[i][i] = arr[i][row - i - 1];
arr[i][row - i - 1] = temp;
}
printf("\n Matrix after Interchanging Diagonals are: \n");
// Nested loop to print the matrix after intercganing the diagonals
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
printf(" %d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
In the above C Program to Interchange Diagonals of a Matrix, we have asked the user to input the number of rows and columns ( size of a two-dimensional matrix ).
Then used the below code to check if the matrix is a square matrix or not. If the condition is false, move the control to the next block for interchanging the matrix's diagonals; otherwise, take the input for the size….
if(row != col)
{
printf("\n Please Enter a Square Matrix !!!" );
goto label1;
}
After this, we created a two-dimensional array of the size of row * col ( a[row][col] )
Next, we have used a nested for loop to iterate through each cell of the array a[row][col]. Conditions in the for loop ( i < row and j < col ) ensure that the compiler will not exceed the maximum limit. The scanf statement inside these for loops asks the user to enter the elements in the matrix such as a[0][0], a[0][1], a[0][2]....
After interchanging the diagonal elements, we used another bunch of nested for loop having conditions ( i < row and j < col ), which is used to print the required matrix.
A matrix is an array or set of numbers arranged in rows and columns in the form of a rectangular array. The numbers are known as the entities or elements of the matrix.
What is the diagonal of a matrix?
The diagonal of a matrix represents the elements present diagonally in the matrix. The elements from the top left corner to the bottom right corner represent the primary diagonal, and the elements from the top right corner to the bottom left corner represent the secondary diagonal.
What is the logic used for interchanging the diagonals?
The idea is to simply iterate from 0 to row-1 and swap the elements present at the position arr[i][i] and arr[i][row - i - 1] for each iteration.
Can we interchange the diagonals of a rectangular matrix?
No, we can only interchange the primary(major) diagonal with the secondary diagonal in a square matrix.
What is a square matrix and a rectangular matrix?
A square matrix is formed by the same number of rows and columns, while in a rectangular matrix, the number of rows and columns are different.
Example of a Square matrix = [[ 1 2 ] [ 3 4 ]]
Example of a Rectangular matrix = [[ 1 2 ][ 3 4 ][ 5 6 ]]
Conclusion
In this article, we have seen the implementation of the C Program to Interchange Diagonals of a Matrix. We had also seen the output of the written program on some random input.
If you want to learn more about C programs, visit the given links below: