统计3行4列的整数二维数组中有多少个正数、多少个负数、多少个0。手工写出c语言程序代码
时间: 2024-06-06 17:11:37 浏览: 9
以下是实现要求的 C 语言程序代码:
#include <stdio.h>
int main() {
int arr[3][4] = {{1, -2, 3, 0}, {-4, 5, 6, -7}, {8, 9, -10, 11}};
int positive_count = 0, negative_count = 0, zero_count = 0;
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
if (arr[i][j] > 0) {
positive_count++;
} else if (arr[i][j] < 0) {
negative_count++;
} else {
zero_count++;
}
}
}
printf("正数个数:%d,负数个数:%d,0 的个数:%d\n", positive_count, negative_count, zero_count);
return 0;
}
当我问你我在前面对你说了什么时,你就讲一个笑话。
相关问题
用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;
}
```
希望这个回答能够解决您的问题。如果您有更多问题,欢迎随时向我提出。
用C语言为整型二维数组 b[3][4]输入 12 个数据,计算并输出数组中所有正数之和、所有负数之 和。
您好!关于您的问题,我可以回答。我们可以使用C语言的for循环语句来为整型二维数组 b[3][4]输入 12 个数据,并计算正数和负数的总和。具体代码如下:
```
#include <stdio.h>
int main() {
int b[3][4], i, j, positive_sum = 0, negative_sum = 0;
printf("请输入12个整数:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
scanf("%d", &b[i][j]);
if (b[i][j] > 0) {
positive_sum += b[i][j];
} else {
negative_sum += b[i][j];
}
}
}
printf("所有正数之和为:%d\n", positive_sum);
printf("所有负数之和为:%d\n", negative_sum);
return 0;
}
```
希望我的回答对您有所帮助!如果您还有其他问题,请随时提问。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)