In this article, we are going to analyze the quantity of positive integers, negative values, and zeros within an array. To determine the sign of a number as positive, negative, or zero, we will implement the if-else conditional statement. The programming language of choice for this task is C++.
In this code snippet, we first request the user to specify the quantity of array elements to be stored in the count variable. Subsequently, we prompt the user to input the array elements which are stored in an integer array named "input". By iterating through a for loop, we traverse the input array starting from index 0 up to count-1, analyzing each element to classify it as positive, negative, or zero.
To determine the quantity of positive, negative, and zero values, we employ the variables nCount, pCount, and zCount. Ultimately, we utilize cout to display the counts of zeros, positive numbers, and negative numbers on the screen.
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).