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

蹲街弑〆低调 提交于 2021-01-20 07:18:29

问题


I'm trying to create a code that counts the number of positive and negative numbers from a given array using a function. For example in the array {-1, 2, -3, 4.5 , 0, .3, -999.99} it's supposed to show 2 positive numbers, and 4 negative numbers and excludes the number 0.

I'm using two counters to keep track of how many negative and positive numbers, a for loop to cycle through the array, but I don't know how to incorporate the boolean parameter when true or false is called on to display the right counter.

My code isn't outputting the right information and any tips would be help on how to improve my code.

#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;
}

回答1:


You cannot return 2 values. Once you return, that function immediately ends. Therefore, countingArray will only return the number of positive numbers you have, as return NumberOfPositives occurs before return NumberOfNegatives.




回答2:


I would have wrote it this way:

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;
    }

Pass the counters by reference so you don't loop array twice. And your problem is you return twice from your function if doing it your way you need to check the boolean flag before returning and return positive or negative counter based on your flag.

Also, you can use std::array instead c type array, that way you can loop through array using iterator and you won't need to pass array size.




回答3:


Something like this would do it:

#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;
}

Encompass everything into a struct and count positive, negative number and eventually 0.

Remember that you have to set each member of the struct to zero before using it (random by default when initialized).




回答4:


You might do, with 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;
}


来源:https://stackoverflow.com/questions/40296508/c-counting-number-of-positive-negative-numbers-from-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!