Finding (positive - negative - odd - even - zero) numbers in an array

Finding (positive - negative - odd - even - zero) numbers in an array

Welcome again to a new lesson and a new example. We're continuing practicing what we learned in the conceptual lesson about arrays ( Taking inputs from the user, storing them in a (1D-2D-3D) array and printing them on the screen in c++ ).

In today's example, we make a simple program in C++ that searches a one-dimensional array for some specific numbers and returns the count of numbers that matches the specified type.

The question says:

Take 20 integer inputs from the user and output the count of the following: 
1. Positive numbers.
2. Negative numbers.
3. Odd numbers.
4. Even numbers.
5. Zeros.

Step 1: Declare a 20-element one-dimensional array.

    int array[20];

Step 2: Prompt the user to enter 20 integer values and store them in the previously declared array - use a (for) loop to pass through the array indexes.

    std::cout << "Enter twenty integers:\n";

    for (int i = 0; i < 20; i++)

    {

        std::cout << "Integer #" << i + 1 << ": ";

        std::cin >> array[i];

    }

Step 3: Declare five integer variables that each of them stores the count of a specified type: (positive - negative - odd - even - zeros) and initialize each of them with values of zeros.
Note: Initially, the count of (positive) numbers is zero, so we put the initial value as zero. If we just write the declaration without giving an initial value, then we would find a random value when we output the final result.

    int countPositive(0), countNegative(0), countOdd(0),
    countEven(0), countZero(0);

Step 4: Create a for loop that passes through the 20 elements of the array. Inside the body of the loop, do the following:

1) First, calculate the count of positive and negative numbers using if/else statements. The if statement has the condition that the value stored in the current index of the array has to be greater than or equals to 0. If "true" then countPositive  is incremented by 1. Otherwise meaning that the value in the current index is less than 0 , so it's negative, then countNegative is incremented by 1.

2) Next, we're going to create if/else statements - inside of the body of the for loop too - that test whether the value stored in the current index of the array is odd or even. The condition of the if statement is that remainder of dividing the current value of the array by 2 is not zero. If yes, the value of countOdd is incremented by one. if no - the remainder is zero - then the number is even, so we're going to increment the value of countEven by one.

3) The last statement in the body of the for loop is an if statement with the condition of that the value of the current index of the array is equal to zero. If "true", we increment the value of countZero by 1. Otherwise, the program does nothing and continues passing to the next index.

    for (int i = 0; i < 20; i++)
    {
if (array[i] >= 0)
    countPositive++;
else
    countNegative++;

if (array[i] % 2 != 0)
    countOdd++;
else
    countEven++;

if (array[i] == 0)
    countZero++;
    }

Step 5: Create five output statements that print the values of (countPositive - countNegative - countOdd - countEven - countZero) on the screen as follows:

    std::cout << "\nPositive numbers: " << countPositive << '\n';
    std::cout << "Negative numbers: " << countNegative << '\n';
    std::cout << "Even numbers: " << countEven << '\n';
    std::cout << "Odd numbers: " << countOdd << '\n';
    std::cout << "Zeros: " << countZero << "\n\n";


Putting it all together:

    
  #include <iostream>

  int main()
  {
      int array[20];

      std::cout << "Enter twenty integers:\n";

      for (int i = 0; i < 20; i++)
      {
          std::cout << "Integer #" << i + 1 << ": ";
          std::cin >> array[i];
      }



      int countPositive(0), countNegative(0), countOdd(0),
          countEven(0), countZero(0);

      for (int i = 0; i < 20; i++)
      {
          if (array[i] >= 0)
              countPositive++;
          else
              countNegative++;

          if (array[i] % 2 != 0)
              countOdd++;
          else
              countEven++;

          if (array[i] == 0)
              countZero++;
      }

      std::cout << "\nPositive numbers: " << countPositive << '\n';
      std::cout << "Negative numbers: " << countNegative << '\n';
      std::cout << "Even numbers: " << countEven << '\n';
      std::cout << "Odd numbers: " << countOdd << '\n';
      std::cout << "Zeros: " << countZero << "\n\n";

      system("pause");
      return 0;
  }

   	

Output:

Finding (positive - negative - odd - even - zero) numbers in a one-dimensional array


Comments

Popular posts from this blog

Two methods for reversing a text string in c++

Taking inputs from the user, storing them in a (1D-2D-3D) array and printing them on the screen in c++