c++从数组中计算正数/负数的数目
C++ counting number of positive/negative numbers from an array
我正在尝试创建一个代码,该代码使用函数从给定数组中计算正数和负数的数量。例如,在数组{-1, 2, -3, 4.5 , 0, .3, -999.99}
中,它应该显示2个正数和4个负数,并且不包括数字0。
我使用两个计数器来跟踪有多少负数和正数,一个for循环来循环遍历数组,但我不知道如何在调用true或false时合并布尔参数以显示正确的计数器。
我的代码没有输出正确的信息,任何提示将有助于如何改进我的代码。
#include <iostream>
using namespace std;
int countingArray(float list[], int size, bool)
{
int NumberOfPositives = 0;
int NumberOfNegatives = 0;
for (int index = 0; index < size; index++) {
if (list[index] > 0) {
if (true) {
NumberOfPositives++;
}
}
else if (list[index] < 0) {
if (false) {
NumberOfNegatives++;
}
}
else if (list[index] == 0)
continue;
}
return NumberOfPositives;
return NumberOfNegatives;
}
int main()
{
float list[] = { -1, 2, -3, 4.5, 0, -3, -999.99 };
cout << "# of Pos. = " << countingArray(list, 7, true) << endl;
cout << "# of Pos. = " << countingArray(list, 7, false) << endl;
system("PAUSE");
return 0;
}
不能设置return
值。一旦你用了return
,这个功能就结束了。因此,countingArray将只返回您拥有的正数的个数,因为return NumberOfPositives
出现在return NumberOfNegatives
之前。
我应该这样写:
void countingArray(float list[], int size, int& positive, int& negative) {
for (int index = 0; index < size; index++)
if (list[index] > 0)
++positive;
else if (list[index] < 0)
++negative;
}
int main() {
float list[] = { -1, 2, -3, 4.5, 0, -3, -999.99 };
int positive = 0;
int negative = 0;
countingArray(list, 7, positive, negative);
cout << "# of Pos. = " << positive << endl;
cout << "# of Pos. = " << negative << endl;
system("PAUSE");
return 0;
}
通过引用传递计数器,这样就不会循环数组两次。你的问题是你从你的函数返回两次,如果按照你的方式做,你需要在返回之前检查布尔标志,并根据你的标志返回正或负计数器。
还可以使用std::array来代替c type array,这样就可以使用迭代器遍历数组,而不需要传递数组大小。
可以这样做:
#include <iostream>
struct PosNeg
{
void reset()
{
p = n = z = 0;
}
int p; // Positive.
int n; // Negative.
int z; // Zero.
};
void pos_neg(float* arr, int sz, PosNeg& count)
{
for (int i = 0; i < sz; ++i)
{
if (arr[i] < 0)
{
count.n++;
}
else if (arr[i] == 0)
{
count.z++;
}
else
{
count.p++;
}
}
}
int main()
{
float arr[] = { 1.0f, 2.0f, 3.0f, 0.0f, -1.0f, -2.0f, -3.0f };
PosNeg pn;
pn.reset();
pos_neg(arr, 7, pn);
std::cout << "Pos: " << pn.p << " Neg: " << pn.n << " Zero: " << pn.z << "n";
std::cin.get();
return 0;
}
将所有内容包含到一个结构体中,计数正数、负数,最终计数0。
请记住,在使用它之前,必须将结构体的每个成员设置为零(初始化时默认为随机)。
可以这样做,使用std:
std::pair<std::size_t, std::size_t> countingNegPos(const float* v, std::size_t size)
{
return { std::count_if(v, v + size, [](auto f){ return f < 0; }),
std::count_if(v, v + size, [](auto f){ return f > 0; })};
}
int main()
{
const float list[] = { -1, 2, -3, 4.5, 0, -3, -999.99 };
auto [neg, pos] = countingArray(list, 7);
std::cout << "# of Neg. = " << neg << std::endl;
std::cout << "# of Pos. = " << pos << std::endl;
}
相关文章:
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- C++数据文件、数组和计算赋值
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 从另一个静态常量数组初始化静态常量数组(只需少量计算)
- 有效地计算多维数组的累积和?
- 通过指针算法计算数组长度
- 计算数组重复次数的组合的有效算法,加起来达到给定的总和
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 使用结构数组计算文本文件中单词的出现次数C++
- 使用 2D 数组计算总计、最高价和最低价
- 二维数组:计算行的总和和列的乘积
- 从字符数组计算平均数
- 练习:使用数组计算方差
- 二维数组计算公式
- 从导入的二维数组计算平均值
- 从数组计算数据的c++问题
- 通过二维数组计算文件中的特定记录
- Python模拟显示慢性能,如何加快数组计算速度
- 通过数组计算文本文件数值数据