如何编写 C 语言程序来查找矩阵的异对角线元素之和?或者如何编写 C 语言程序来查找多维数组的异对角线元素之和,并附有示例。

C 语言查找矩阵异对角线元素之和示例 1
此程序允许用户输入矩阵的行数和列数。接下来,我们将使用 For 循环在此矩阵中计算异对角线元素之和。
/* C Program to find Sum of Opposite Diagonal Elements of a Matrix */
#include<stdio.h>
int main()
{
int i, j, rows, columns, a[10][10], Sum = 0;
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", &a[rows][columns]);
}
}
for(rows = 0; rows < i; rows++)
{
Sum = Sum + a[rows][i - rows - 1];
}
printf("\n The Sum of Opposite Diagonal Elements of a Matrix = %d", Sum );
return 0;
}

在这个 C 语言程序中,用于查找矩阵异对角线元素之和,我们声明了一个大小为 10*10 的二维数组 Multiplication。
下面的语句要求用户输入矩阵的大小(行数和列数。例如 2 行,3 列 = a[2][3])
printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);
接下来,我们使用 for 循环遍历 a[3][3] 矩阵中的每个单元格。for 循环中的条件 ((rows < i) 和 (columns < j)) 将确保编译器不会超出矩阵限制。否则,矩阵将溢出
For 循环内的 C 语言 scanf 语句会将用户输入的值存储到每个单独的数组元素中,例如 a[0][0],a[0][1],……。
for(rows = 0; rows < i; rows++).
{
for(columns = 0; columns < j; columns++)
{
scanf("%d", &a[rows][columns]);
}
}
下一行,我们还有另一个 for 循环来查找矩阵的对角线元素之和
for(rows = 0; rows < i; rows++)
{
Sum = Sum + a[rows][i - rows - 1];
}
用户输入的 C 语言程序用于查找矩阵异对角线元素之和的值为:a[3][3] = {{10, 20, 30}, { 40, 50, 60}, {70, 80, 90}}
行第一次迭代: for(rows = 0; rows < 3; 0++)
条件 (0 < 3) 为 True。
Sum = Sum + a[rows][i – rows – 1]
Sum = Sum + a[0][3 – 0 – 1] => Sum + a[0][2]
Sum = 0 + 30 = 30
第二行迭代:for(rows = 1; rows < 3; 1++)
条件 (1 < 3) 为真。
Sum = Sum + a[1][3 – 1 – 1] => Sum + a[1][1]
Sum = 30 + 55 = 85
第二行迭代:for(rows = 2; rows < 3; 2++)
条件 (2 < 3) 为真。
Sum = Sum + a[2][3 – 2 – 1] => Sum + a[2][0]
Sum = 85 + 95 = 180
接下来,rows 值递增。递增后,for 循环内的条件 (rows < 3) 将失败。因此,它将退出循环。
最后,我们使用 printf 语句将总 Sum 作为输出打印出来
C 语言查找矩阵异对角线元素之和示例 2
这个C 语言程序用于计算矩阵异对角线元素之和,与上面相同,但这次我们稍微改变了算法。
/* C Program to find Sum of Opposite Diagonal Elements of a Matrix */
#include<stdio.h>
int main()
{
int i, j, rows, columns, a[10][10], Sum = 0;
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", &a[rows][columns]);
}
}
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j; columns++)
{
if(rows + columns == ((i + 1) - 2))
Sum = Sum + a[rows][columns];
}
}
printf("\n The Sum of Opposite Diagonal Elements of a Matrix = %d", Sum );
return 0;
}
C 语言矩阵异对角线之和输出
Please Enter Number of rows and columns : 3 3
Please Enter the Matrix Elements
1 2 3
4 5 6
7 8 15
The Sum of Opposite Diagonal Elements of a Matrix = 15
用户输入的值为:a[3][3] = {{1, 2, 3}, { 4, 5, 6}, {7, 8, 15}}
行第一次迭代: for(rows = 0; rows < 3; 0++)
条件 (0 < 3) 为真。因此,它将进入第二个 for 循环
列第一次迭代: for(columns = 0; 0 < 3; 0++)
条件 (columns < 3) 为真。因此,它将开始执行If 语句
if(rows + columns == ((i + 1) – 2))
=> if(0 + 0 == ((3 + 1) – 2)) – 条件为假
列第二次迭代:for(columns = 1; 1 < 3; 1++)
条件 (1 < 3) 为真。
if(rows + columns == ((i + 1) – 2))
=> if(0 + 1 == ((3 + 1) – 2)) – 条件为假
第二列迭代:for(columns = 2; 2 < 3; 2++)
条件 (1 < 3) 为真。
if(rows + columns == ((i + 1) – 2))
=> if(0 + 2 == ((3 + 1) – 2)) – 条件为真
Sum = Sum + a[rows][columns] => Sum + a[0][2]
Sum = 0 + 3 = 3
对剩余迭代执行相同操作,其中 rows = 1 和 rows = 2