用c语言解决:为整型二维数组 b[3][4]输入 12 个数据,计算并输出数组中所有正数之和、所有负数之和。 输入格式: 在一行中输入12个数,中间用空格分隔 输出格式: 在一行中输出正数之和、负数之和,中间用逗号分隔 输入样例: 在这里给出一组输入。例如: 12 12 1 3 12 -12 23 -98 23 23 23 23 输出样例: 在这里给出相应的输出。例如: 155,-110
时间: 2023-06-09 07:05:21 浏览: 47
输入方法:
```c
int b[3][4];
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
scanf("%d",&b[i][j]);
}
}
```
计算正数和负数:
```c
int sum_positive=0,sum_negative=0;
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
if(b[i][j]>0){
sum_positive+=b[i][j];
}
else{
sum_negative+=b[i][j];
}
}
}
```
输出结果:
```c
printf("%d,%d",sum_positive,sum_negative);
```
相关问题
用c语言编程:为整型二维数组 b [3][4]输入12个数据,计算并输出数组中所有正数之和、所有负数之和。
可以使用以下代码实现:
```c
#include <stdio.h>
int main() {
int b[3][4];
int i, j, positive_sum = 0, negative_sum = 0;
// 输入12个数据
printf("请输入12个整数:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
scanf("%d", &b[i][j]);
}
}
// 计算正数和负数之和
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
if (b[i][j] > 0) {
positive_sum += b[i][j];
} else if (b[i][j] < 0) {
negative_sum += b[i][j];
}
}
}
// 输出结果
printf("所有正数之和为:%d\n", positive_sum);
printf("所有负数之和为:%d\n", negative_sum);
return 0;
}
```
这段代码先定义了一个 3 行 4 列的整型二维数组 `b`,然后通过两个 for 循环输入数组中的 12 个数据。
接着,再使用两个 for 循环计算数组中所有正数之和和所有负数之和,并分别存储在 `positive_sum` 和 `negative_sum` 变量中。
最后,使用 printf 函数输出计算结果即可。
用c语言为整型二维数组 b[3][4]输入 12 个数据,计算并输出数组中所有正数之和、所有负数之和
好的,我可以回答这个问题。以下是用 C 语言计算数组 b[3][4] 中所有正数之和和所有负数之和的代码:
```c
#include <stdio.h>
int main() {
int b[3][4];
int i, j;
int pos_sum = 0, neg_sum = 0;
// 输入数组
printf("请输入12个整数:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
scanf("%d", &b[i][j]);
}
}
// 计算正数和负数之和
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
if (b[i][j] > 0) {
pos_sum += b[i][j];
}
else if (b[i][j] < 0) {
neg_sum += b[i][j];
}
}
}
// 输出结果
printf("所有正数之和为:%d\n", pos_sum);
printf("所有负数之和为:%d\n", neg_sum);
return 0;
}
```
希望这个回答能够解决您的问题。如果您有更多问题,欢迎随时向我提出。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)