c++统计数组中0的个数
作者:野牛程序员:2024-07-03 11:32:08 C++阅读 2841
c++统计数组中0的个数
可以使用C++代码来统计数组中0的个数。以下是一个示例程序:
#include <iostream>
int countZeros(int arr[], int size) {
int count = 0;
for(int i = 0; i < size; ++i) {
if(arr[i] == 0) {
++count;
}
}
return count;
}
int main() {
int arr[] = {1, 0, 2, 0, 3, 0, 4};
int size = sizeof(arr) / sizeof(arr[0]);
int zeroCount = countZeros(arr, size);
std::cout << "数组中0的个数是: " << zeroCount << std::endl;
return 0;
}在这个示例中,定义了一个名为countZeros的函数,该函数接受一个数组和数组的大小作为参数,并返回数组中0的个数。在main函数中,创建一个示例数组并调用countZeros函数来计算数组中0的个数,然后输出结果。
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

