counting negatives OR positives in array

The homework is to count the number of positive numbers in an array if bool is "true" and count the negative numbers if bool is "false". I am only allowed one loop.
My code will return the positive count correctly but not the negative. what should I fix?
-----------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;


int countingArray(float list[], int size, bool)
{
	
	int positive = 0;
	int negative = 0;
	
		for (int i = 0; i < size; i++) {
			if (list[i] > 0)
				positive++;
			else if (list[i] < 0)
				negative++;
		}
		if (true)
			return positive;
		else if (false)
			return negative;
		
}

int main()
{
	float list[] = { -1,-2,-6,-5,7,8,9 };
	cout << countingArray(list, 7,false) << endl;

	system("PAUSE");
	return 0;
}
Last edited on
Your function doesn't use the bool parameter. Isn't that what should decide what is returned?


Note:
1
2
3
4
		if (true) // this condition is always true
			return positive;
		else if (false)
			return negative;

In other words, those four lines do the same as:
return positive;

If true would miraculously be false, then you would go to the else branch and there you would test if (false), which is never true.


PS. What about 0? Is it positive, negative, neither?
I realized i did not name my bool var. seems to work now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;


int countingArray(float list[], int size, bool b)
{
	
	int positive = 0;
	int negative = 0;
	
		for (int i = 0; i < size; i++) {
			if (list[i] > 0)
				positive++;
			else if (list[i] < 0)
				negative++;
		}
		if (b==true)
			return positive;
		else if (b==false)
			return negative;
		
}











int main()
{
	float list[] = { -1,-2,-6,-5,7,8,9 };
	cout << countingArray(list, 7,true) << endl;

	system("PAUSE");
	return 0;
}
Hello roundtwo,

Just for something to consider:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

int countingArray(float list[], int size, bool posNeg)
{
	
	int positive = 0;
	int negative = 0;
	
		for (int i = 0; i < size; i++)
		{
			if (list[i] > 0)
				positive++;
			else  // <--- No else if needed.
				negative++;
		}
		
		if (posNeg)
			return positive;
		// <--- No else or else if needed.
		return negative;
}

int main()
{
	float list[] = { -1,-2,-6,-5,7,8,9 };

	cout << countingArray(list, 7, false) << endl;

	system("PAUSE");

	return 0;
}


Andy
Thank you Andy and Keskiverto for you help!

Andy i think i need line 15 to read "else if", because if b==false and there is a 0 in the array, it will count it as a negative.

unless there is another way.
Last edited on
There are many other ways and this is a long-winded one which incorporates the bool variable. You can probably see the this bool variable is unnecessary because the first if condition effectively serves the same purpose.

I wonder whether 0 is positive or negative?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

int countingArray(float list[], int size)
{
    bool is_positive{false};
    int positive = 0;
    
    for (int i = 0; i < size; i++)
    {
        if (list[i] >= 0)
        {
            is_positive = true;
        }
        
        if(is_positive == true)
            positive++;
    }
    
    return positive;
}

int main()
{
    float list[] = { -1,-2,-6,-5,7,8,9 };
    
    int positive = countingArray(list, 7);
    int negative = 7 - positive;
    
    cout <<  positive << ' ' << negative << endl;
    return 0;
}


3 4
Program ended with exit code: 0
I.e. ( Probably same as already posted here, if not, why not? )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

int countingArray(float list[], int size)
{
    int positive = 0;
    
    for (int i = 0; i < size; i++)
    {
        if (list[i] >= 0)
            positive++;
    }
    
    return positive;
}

int main()
{
    float list[] = { -1,-2,-6,-5,7,8,9 };
    
    int positive = countingArray(list, 7);
    int negative = 7 - positive;
    
    cout <<  positive << ' ' << negative << endl;
    return 0;
}
One of the other approaches is to count only one thing and do the decision by choosing
the operator.
1
2
3
4
5
6
7
8
9
10
11
12
#include <functional> // https://en.cppreference.com/w/cpp/utility/functional/less

int countingArray( float list[], int size, bool posNeg ) {
  int count = 0;
  auto op = posNeg ? std::less : std::greater ;
  for ( int i = 0; i < size; ++i ) {
    if ( op( 0, list[i] ) ) {
      ++count;
    }
  }
  return count;
}

Or
1
2
3
4
5
6
7
8
9
#include <algorithm> // https://en.cppreference.com/w/cpp/algorithm/count

int countingArray( float list[], int size, bool posNeg ) {
  if ( posNeg ) {
    return std::count_if( list, list+size, [](int x){ return 0 < x; } );
  } else {
    return std::count_if( list, list+size, [](int x){ return x < 0; } );
  }
}

Oh no, the latter has two loops. Helper functions were allowed?
1
2
3
4
5
6
7
#include <algorithm>

bool is_positive( int x ){ return 0 < x; }
bool is_negative( int x ){ return x < 0; }
int countingArray( float list[], int size, bool posNeg ) {
  return std::count_if( list, list+size, posNeg ? is_positive : is_negative );
}

Way(s) to do this really depend upon what a 'positive' number is classified as. The mathematical definition are numbers greater than 0 (but not equal). However, some classify positive as not negative (negative being less than 0) - which includes 0. What definition is being used here?

@keskiverto
In the above post, your solution 1) doesn't compile with VS2019 - error C2275: 'std::less': illegal use of this type as an expression

[probably because less and greater are different types (they are defined as struct) and the type of op has to be known at compile time?]
Last edited on
Assuming positive is > 0, then just using a simple loop:

1
2
3
4
5
6
7
8
int countingArray(float list[], int size, bool posNeg) {
	int count {};

	for (int i = 0; i < size; ++i)
		count += (posNeg ? list[i] > 0 : list[i] < 0);

	return count;
}


or if using count_if:

1
2
3
int countingArray(float list[], int size, bool posNeg) {
	return std::count_if(list, list + size, [posNeg](auto n) {return posNeg ? n > 0 : n < 0; });
}

Last edited on
Topic archived. No new replies allowed.