C Program to Interchange Diagonals of a Matrix

How to write a C Program to Interchange Diagonals of a Matrix with an example?.

C program to interchange Diagonals of a Matrix 1

C program to Interchange Diagonals of a Matrix Example

This program allows the user to enter the number of rows and columns of a Matrix. Next, we are going to interchange the diagonals of the given matrix using For Loop.

#include<stdio.h>

int main()
{
int i, j, rows, columns, arr[10][10], temp;

printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);

printf("\n Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &arr[rows][columns]);
}
}

if(rows == columns)
{
for(rows = 0; rows < i; rows++)
{
temp = arr[rows][rows];
arr[rows][rows] = arr[rows][i - rows - 1];
arr[rows][i - rows - 1] = temp;
}

printf("\n Matrix Elemnts after Interchanging Diagonals are: \n");
for(rows = 0; rows < j; rows++)
{
for(columns = 0; columns < i; columns++)
{
printf("%d \t ", arr[rows][columns]);
}
printf("\n");
}
}
else
{
printf("\n The Matrix that you entered is Not a Square matrix" );
}

return 0;
}
C program to interchange Diagonals of a Matrix 2

In this Program, We declared single Two dimensional arrays Multiplication of size of 10 * 10. The below C Programming statements asks the User to enter the Matrix size (Number of rows and columns. For instance 2 Rows, 2 Columns = a[2][2] )

printf("\n Please Enter Number of rows and columns  :  ");
scanf("%d %d", &i, &j);

Next, we used for loop to iterate each cell present in a[2][2] matrix. Conditions inside C for loop ((rows < i) and (columns < j)) will ensure the compiler, not to exceed the C Matrix limit. Otherwise, the matrix will overflow. The scanf statement inside the for loop will store the user entered values in every individual array element such as a[0][0], a[0][1], …..

for(rows = 0; rows < i; rows++).
{
  for(columns = 0; columns < j; columns++)
   {
     scanf("%d", &a[rows][columns]);
   }
}

Next, we are using the If Else statement to check whether the given matrix is a Square matrix or Not. If it is True, then the compiler will interchange the diagonals. Otherwise, it will print a message present in else block.

for(rows = 0; rows < i; rows++)
{
	temp = arr[rows][rows];
	arr[rows][rows] = arr[rows][i - rows - 1];
	arr[rows][i - rows - 1] = temp;
}

Next, we used another for loop to print the array. The user inserted values for C Program to Interchange Diagonals of a Matrix are: a[3][3] = {{10, 20, 30}, { 40, 50, 60},{70, 80, 90}}

Row First Iteration: for(rows = 0; rows < 3; 0++)
The condition (0 < 3) is True.

Column First Iteration: for(columns = 0; 0 < 3; 0++)
The condition (columns < 3) is True.
temp = arr[rows][rows] = 10
arr[0][0] = arr[0][3 – 0 – 1] => arr[0][2]
arr[0][0] = 30
arr[0][3 – 0 – 1] => arr[0][2] = temp
arr[0][2] = 10
Do the same for the remaining iterations.

C program to Interchange Diagonals of a Matrix Example 2

This C program for matrix diagonal interchange is same as above but this time we used C functions concept to operate the code.

#include<stdio.h>
void interchnage_Diagonals(int arr[10][10], int i, int j);

int main()
{
int i, j, rows, columns, arr[10][10], temp;

printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);

printf("\n Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &arr[rows][columns]);
}
}

if(rows == columns)
{
interchnage_Diagonals(arr, i, j);
}
else
{
printf("\n The Matrix that you entered is Not a Square matrix" );
}

return 0;
}

void interchnage_Diagonals(int arr[10][10], int i, int j)
{
int rows, columns, temp;
for(rows = 0; rows < i; rows++)
{
temp = arr[rows][rows];
arr[rows][rows] = arr[rows][i - rows - 1];
arr[rows][i - rows - 1] = temp;
}

printf("\n Matrix Elemnts after Interchanging Diagonals are: \n");
for(rows = 0; rows < j; rows++)
{
for(columns = 0; columns < i; columns++)
{
printf("%d \t ", arr[rows][columns]);
}
printf("\n");
}
}

The Matrix Diagonal interchange output.

 Please Enter Number of rows and columns  :  2 2

 Please Enter the Matrix Elements 
10 20
30 40

 Matrix Elemnts after Interchanging Diagonals are: 
20 	 10 	 
40 	 30 

Also Read