Count Positive and Negative Numbers in C++

Beginner

Introduction

In this lab, you will learn how to write a C++ program to count the number of positive and negative numbers in an array. The program takes an array of integers as input from the user and counts the number of positive and negative integers in the array.

Create a new C++ file

Create a new file named main.cpp in the ~/project directory using the following command:

touch ~/project/main.cpp

Include necessary header files

The program requires the use of the iostream library to perform input and output operations with the user. The following code includes this library in the program:

#include <iostream>
using namespace std;

Declare variables and ask for user input

The program first declares integer variables to hold the number of positive, negative and zero integers in the array. It then prompts the user to enter the size of the array and subsequently the array elements. The following code block performs this operation:

int main()
{
  int a[100], i, n, zero = 0, pos = 0, neg = 0;

  cout << "Enter the size of an array:\n";
  cin >> n;

  cout << "Enter the elements:\n";
  for(i = 0; i < n; i++)
  {
    cin >> a[i];
  }
}

Count number of positive, negative and zero integers

The program then loops through each array element, checking if they are positive, negative or zero. It increments the appropriate counter variables for each respective value encountered. The following code block performs this operation:

  for(i = 0; i < n; i++)
  {
    if(a[i] > 0)
      pos++;
    else if(a[i] < 0)
      neg++;
    else
      zero++;
  }

Display number of positive, negative and zero integers

Finally, the program displays the number of positive, negative and zero integers present in the array. The following code block performs this operation:

  cout << "\nPositive numbers: " << pos << endl;
  cout << "Negative numbers: " << neg << endl;
  cout << "Zeroes: " << zero << endl;

  return 0;
}

Compile and run the program

Now, compile the program using the following command:

g++ ~/project/main.cpp -o main

After a successful compilation, run the program using the following command:

./main

Complete Code

The complete code for the program is shown below:

#include <iostream>
using namespace std;

int main()
{
  int a[100], i, n, zero = 0, pos = 0, neg = 0;

  cout << "Enter the size of an array:\n";
  cin >> n;

  cout << "Enter the elements:\n";
  for(i = 0; i < n; i++)
  {
    cin >> a[i];
  }

  for(i = 0; i < n; i++)
  {
    if(a[i] > 0)
      pos++;
    else if(a[i] < 0)
      neg++;
    else
      zero++;
  }

  cout << "\nPositive numbers: " << pos << endl;
  cout << "Negative numbers: " << neg << endl;
  cout << "Zeroes: " << zero << endl;

  return 0;
}

Summary

In this lab, you have learned how to write a C++ program to count the number of positive and negative numbers in an array. The program takes an array of integers as input from the user and counts the number of positive and negative integers in the array. This program can be helpful when analyzing data arrays.

Other Tutorials you may like