c++从数组中计算正数/负数的数目

C++ counting number of positive/negative numbers from an array

本文关键字:数组 计算 c++      更新时间:2023-10-16

我正在尝试创建一个代码,该代码使用函数从给定数组中计算正数和负数的数量。例如,在数组{-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;
}