In this post, we will count the number of positive integers, negative numbers, and zeroes in an array. To evaluate whether a number is positive, negative, or zero, the if-else statement will be applied. We will be using C++.
In the following code, we initially prompt the user to input the number of array elements to be stored in the count variable. Next we ask user to input array elements and retain in an integer array "input". Using a for loop, we scan the input array from index 0 to count-1 and examine each array element to determine whether it is positive, negative, or zero.
To count the number of positive, negative, and zero values, we use the variables nCount, pCount, and zCount. Finally, we use cout to output the number of zeroes, positive and negative values on the display.
C++ Program:
#include <iostream>
using namespace std;
int main(){
int input[100],count,i,nCount=0,pCount=0,zCount=0;
cout << "Enter Number of Elements in Array\n";
cin >> count;
cout << "Enter " << count << " numbers \n";
for(i = 0; i < count; i++){
cin >> input[i];
}
for(i = 0; i < count; i++){
if(input[i] < 0) {
nCount++;
} else if(input[i] > 0){
pCount++;
} else {
zCount++;
}
}
cout << "Negative Numbers : " << nCount << endl;
cout << "Positive Numbers : " << pCount << endl;
cout << "Zeroes : " << zCount << endl;
return 0;
}
Output
Enter Number of Elements in Array
8
Enter 8 numbers
-65
12
-5
6
0
-22
0
38
Negative Numbers : 3
Positive Numbers : 3
Zeroes : 2
Time complexity will be O(n).
Auxiliary space will be O(1).
By Using Recursion Method
C++ Program:
#include<bits/stdc++.h>
using namespace std;
int CountPositive(int begin,int arr[],int n,int pos_count) {
if (begin==n)
return pos_count;
if (arr[begin]>=0)
pos_count++;
return CountPositive(begin+1,arr,n,pos_count);
}
void printArray(int arr[],int n) {
cout<<"Array: ";
for(int i = 0; i<n; i++) {
cout << arr[i] <<" ";
}
cout << "\n";
}
int main()
{
int arr[] = {-9,7,-5,3,2 };
int n;
n = sizeof(arr) / sizeof(arr[0]);
printArray(arr, n);
int p_count = CountPositive(0,arr,n,0);
cout << "Count of Positive elements = " << p_count << ", ";
cout << "Count of Negative elements = " << n - p_count;
return 0;
}
Output
Array: -9 7 -5 3 2
Count of Positive elements = 3, Count of Negative elements = 2
Time complexity will be O(n).
Auxiliary space will be O(n).
Alternative method utilizing the C++ Standard Template Library (STL) and the 'count_if'function:
C++ Program:
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int arr[] = {-9,7,-5,3,2};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Array: ";
for(int i = 0; i<n; i++){
cout << arr[i] << " ";
}
cout<<"\n";
int pos_count = count_if(arr, arr + n, [](int x) { return x >= 0; });
int neg_count = count_if(arr, arr + n, [](int x) { return x < 0; });
cout << "Count of Positive elements =" << pos_count<<", ";
cout << "Count of Negative elements = " << neg_count;
return 0;
}
Output
Array: -9 7 -5 3 2
Count of Positive elements = 3, Count of Negative elements = 2
Time complexity will be O(n).
Auxiliary space will be O(n).