如何编写一个C语言程序来交换矩阵的对角线?附带示例?

C语言:交换矩阵对角线示例程序
此程序允许用户输入矩阵的行数和列数。接下来,我们将使用For循环交换给定矩阵的对角线。
#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;
}

在此程序中,我们声明了一个大小为10*10的二维数组。下面的C编程语句会要求用户输入矩阵的大小(行数和列数。例如2行,2列=a[2][2])
printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);
接下来,我们使用for循环遍历a[2][2]矩阵中的每个单元格。for循环中的条件((rows < i) and (columns < j))将确保编译器不会超出矩阵的限制。否则,矩阵将溢出。for循环内的scanf语句将把用户输入的值存储在每个单独的数组元素中,例如a[0][0],a[0][1],……。
for(rows = 0; rows < i; rows++).
{
for(columns = 0; columns < j; columns++)
{
scanf("%d", &a[rows][columns]);
}
}
接下来,我们使用If Else语句来检查给定的矩阵是否为方阵。如果为真,编译器将交换对角线。否则,它将打印else块中的消息。
for(rows = 0; rows < i; rows++)
{
temp = arr[rows][rows];
arr[rows][rows] = arr[rows][i - rows - 1];
arr[rows][i - rows - 1] = temp;
}
接下来,我们使用另一个for循环来打印数组。用户为C语言交换矩阵对角线程序输入的值为:a[3][3] = {{10, 20, 30}, { 40, 50, 60},{70, 80, 90}}
行第一次迭代: for(rows = 0; rows < 3; 0++)
条件 (0 < 3) 为 True。
列第一次迭代: for(columns = 0; 0 < 3; 0++)
条件 (columns < 3) 为真。
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
对剩余的迭代执行相同的操作。
C语言:交换矩阵对角线示例程序 2
这个C语言矩阵对角线交换程序与上面相同,但这次我们使用了函数概念来操作代码。
#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");
}
}
矩阵对角线交换输出。
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