Input a 4*3 table, calculate the sum of each row.
时间: 2024-03-26 21:42:26 浏览: 95
Sure, here is a Python code that can calculate the sum of each row in a 4*3 table:
```python
table = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]]
row_sums = []
for row in table:
row_sum = sum(row)
row_sums.append(row_sum)
print(row_sums)
```
Output:
```
[6, 15, 24, 33]
```
In the above code, `table` is a 4*3 table (a list of lists) with 4 rows and 3 columns. We then iterate over each row in the table, calculate the sum of the row using the `sum()` function, and append the row sum to a new list called `row_sums`. Finally, we print out the `row_sums` list, which contains the sum of each row in the table.
相关问题
Input a 4*3 table, calculate the sum of each row.c
Sure, here is a Python code that can calculate the sum of each row in a 4*3 table:
```python
table = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]]
row_sums = []
for row in table:
row_sum = sum(row)
row_sums.append(row_sum)
print(row_sums)
```
Output:
```
[6, 15, 24, 33]
```
In the above code, `table` is a 4*3 table (a list of lists) with 4 rows and 3 columns. We then iterate over each row in the table, calculate the sum of the row using the `sum()` function, and append the row sum to a new list called `row_sums`. Finally, we print out the `row_sums` list, which contains the sum of each row in the table.
Input a 4*3 table, calculate the sum of each row. Inut: 12 integers in 4*3 tabel Output: 4 integers with each one in single line.c语言实现
好的,以下是C语言实现的代码:
```c
#include <stdio.h>
int main()
{
int table[4][3]; // 定义4*3的表格
int sum[4] = {0}; // 定义每行的和,初始值为0
// 读入表格
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d", &table[i][j]);
sum[i] += table[i][j]; // 计算每行的和
}
}
// 输出每行的和
for (int i = 0; i < 4; i++)
{
printf("%d\n", sum[i]);
}
return 0;
}
```
这个程序先定义了一个4*3的表格和一个长度为4的数组,用于存储每行的和。然后通过循环读入表格中的12个整数,并计算每行的和。最后再循环输出每行的和即可。
阅读全文