mirror of
https://github.com/renbaoshuo/202401-programming-assignments.git
synced 2024-12-16 15:44:39 +00:00
【实践课外】13.指针1/7-1 求矩阵每行元素的和.c
This commit is contained in:
parent
0f39a3a688
commit
b3f466c6ed
37
【实践课外】13.指针1/7-1 求矩阵每行元素的和.c
Normal file
37
【实践课外】13.指针1/7-1 求矩阵每行元素的和.c
Normal file
@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int row_sum(int *a, int n) {
|
||||
int s = 0;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
s += a[i];
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void mat_sum(int mat[][10], int n, int m, int *sum) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
sum[i] = row_sum(mat[i], m);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n, m, mat[10][10], sum[10];
|
||||
|
||||
scanf("%d%d", &n, &m);
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
scanf("%d", &mat[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
mat_sum(mat, n, m, sum);
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
printf("%d\n", sum[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
29
【实践课外】13.指针1/7-1 求矩阵每行元素的和.md
Normal file
29
【实践课外】13.指针1/7-1 求矩阵每行元素的和.md
Normal file
@ -0,0 +1,29 @@
|
||||
# 7-1 求矩阵每行元素的和
|
||||
|
||||
本题要求编写程序,使用指针方式求一个给定的m×n矩阵各行元素之和。
|
||||
|
||||
### 输入格式:
|
||||
|
||||
输入第一行给出两个正整数m和n(1≤m,n≤6),再输入m行数据,每行n个整数,每个整数之间用空格分隔。
|
||||
|
||||
### 输出格式:
|
||||
|
||||
输出m个整数,每行1个数。
|
||||
|
||||
### 输入样例:
|
||||
|
||||
|
||||
|
||||
```in
|
||||
2 3
|
||||
1 2 3
|
||||
4 5 6
|
||||
```
|
||||
|
||||
### 输出样例:
|
||||
|
||||
|
||||
```out
|
||||
6
|
||||
15
|
||||
```
|
Loading…
Reference in New Issue
Block a user