C 语言查找矩阵每行每列之和的程序

如何编写C语言程序来查找矩阵每行每列之和?或者,用一个例子编写一个C语言程序来查找多维数组每行每列之和。

Sum of each row and column of a Matrix 1

C 语言查找矩阵每行每列之和的程序 示例 1

此程序允许用户输入矩阵的行数和列数。接下来,我们将使用 For 循环计算此矩阵中每行每列元素的和。

#include<stdio.h>

int main()
{
int i, j, rows, columns, a[10][10], Sum;

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 = 0;
for(columns = 0;columns < j; columns++)
{
Sum = Sum + a[rows][columns];
}
printf("\n The Sum of Elements of a Rows in a Matrix = %d", Sum );
}

for(rows = 0; rows < i; rows++)
{
Sum = 0;
for(columns = 0;columns < j; columns++)
{
Sum = Sum + a[columns][rows];
}
printf("\n The Sum of Elements of a Columns in a Matrix = %d", Sum );
}

return 0;
}
C Program to find Sum of each row and column of a Matrix 2

在这个 C 语言程序中,我们声明了一个大小为 10 * 10 的二维数组乘法。
下面的语句要求用户输入矩阵大小(行数和列数。例如 2 行,3 列 = a[2][3])

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

接下来,我们使用 C 编程的 for 循环来迭代 a[3][3] 矩阵中的每个单元格。for 循环中的条件((rows < i) 和 (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]);
   }
}

在下一行,我们还有一个 for 循环来查找矩阵行元素的和。

for(rows = 0; rows < i; rows++)
{
  		Sum = 0;
  		for(columns = 0;columns < j; columns++)
  		{
  			Sum = Sum + a[rows][columns];
		}
   		printf("\n The Sum of Elements of a Rows in a Matrix =  %d", Sum );
}

用户为 C 语言程序输入的矩阵每行每列之和的值为:a[3][3] = {{10, 20, 30}, { 40, 50, 60}, {70, 80, 90}}

行第一次迭代: for(rows = 0; rows < 3; 0++)
条件 (0 < 3) 为 True。
Sum = 0

列第一次迭代: for(columns = 0; 0 < 3; 0++)
条件 (columns < 3) 为真。因此,它将开始执行循环内的语句。
Sum = Sum + a[rows][columns]
Sum  = Sum + a[0][0] => 0 + 10 = 10

列第二次迭代:for(columns = 1; 1 < 3; 1++)
条件 (columns < 3) 为真。
Sum = Sum + a[0][1] => 10 + 20 = 30

列第三次迭代:for(columns = 2; 2 < 3; 2++)
条件 (columns < 3) 为真。
Sum = Sum + a[0][2] => 30 + 30 = 60

接下来,列的值将递增。递增后,第二个 for 循环(columns < 3)内的条件将失败。因此,它将退出循环。

接下来,我们使用 Printf 语句打印 Sum。在此之后,行值将增加到 1,Sum 将变为 0。

对 rows = 1 和 rows = 2 重复上述步骤

在下一行,我们还有一个 for 循环来查找矩阵列元素的和。

for(rows = 0; rows < i; rows++)
{
  		Sum = 0;
  		for(columns = 0;columns < j; columns++)
  		{
  			Sum = Sum + a[columns][rows];
		}
   		printf("\n The Sum of Elements of a Columns in a Matrix =  %d", Sum );
}

行第一次迭代: for(rows = 0; rows < 3; 0++)
条件 (0 < 3) 为 True。
Sum = 0

列第一次迭代: for(columns = 0; 0 < 3; 0++)
条件 (columns < 3) 为真。因此,它将开始执行循环内的语句。
Sum = Sum + a[columns][rows]
Sum  = Sum + a[0][0] => 0 + 10 = 10

列第二次迭代:for(columns = 1; 1 < 3; 1++)
条件 (columns < 3) 为真。
Sum = Sum + a[1][0] => 10 + 40 = 50

列第三次迭代:for(columns = 2; 2 < 3; 2++)
条件 (columns < 3) 为真。
Sum = Sum + a[2][0] => 50 + 70 = 120

接下来,列的值将递增。递增后,第二个 for 循环(columns < 3)内的条件将失败。因此,它将退出循环。

接下来,我们使用 Printf 语句打印 Sum。在此之后,行值将增加到 1,Sum 将变为 0。

对 rows = 1 和 rows = 2 重复上述步骤

C 语言查找矩阵每行每列之和的程序 示例 2

此矩阵行和列之和的程序与上述相同,但这次我们使用两个不同的函数组织了代码。

#include<stdio.h>

void AddRows(int arr[10][10], int i, int j);
void AddColumns(int arr[10][10], int i, int j);

int main()
{
int i, j, rows, columns, a[10][10], RowSum, ColumnSum;

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]);
}
}

AddRows(a, i, j);
AddColumns(a, i, j);

return 0;
}

void AddRows(int arr[10][10], int i, int j)
{
int rows, columns, Sum = 0;
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j; columns++)
{
Sum = Sum + arr[rows][columns];
}
printf("\n The Sum of Elements of a Rows in a Matrix = %d", Sum );
}
}

void AddColumns(int arr[10][10], int i, int j)
{
int rows, columns, Sum = 0;
for(columns = 0; columns < j; columns++)
{
for(rows = 0; rows < i; rows++)
{
Sum = Sum + arr[rows][columns];
}
printf("\n The Sum of Elements of a Columns in a Matrix = %d", Sum );
}
}
 Please Enter Number of rows and columns  :  3 3

 Please Enter the Matrix Elements 
25 35 45
44 75 95
125 86 95

 The Sum of Elements of a Rows in a Matrix =  105
 The Sum of Elements of a Rows in a Matrix =  319
 The Sum of Elements of a Rows in a Matrix =  625
 The Sum of Elements of a Columns in a Matrix =  194
 The Sum of Elements of a Columns in a Matrix =  390
 The Sum of Elements of a Columns in a Matrix =  625