Showing posts with label positive and negative numbers counter. Show all posts
Showing posts with label positive and negative numbers counter. Show all posts

Wednesday, August 6, 2014

Positive and Negative Numbers Counter in C++

One of the basic programming problems that I have developed for my C++ programming class here in the Philippines is to count the number of positive and negative numbers being given by the user of our program. My intention of creating this program is to introduce my students in my programming class how to understand and learn how to use one dimensional array as their data structure to display the positive and negative numbers in our program.

Normally arrays is one of the simplest data structure in my own opinion that can be learned easy by apply person interested in computer programming. We can define array as a list of values that has the same data type and having the same variable name also. It is very easy to manage data if we are using arrays because we are only dealing with one variable name but we can identify different values in our array by its index or elements that we access in our program. 

If order for us to determine if the number is positive is by having using this condition in our program  if (numbers[y] >=0) this condition tells us the if number[x] is greater the zero then it will display all positive numbers in our program. In our case I consider zero as positive number in our example. For checking for negative numbers and display the values of our screen I am using this condition if (numbers[y] < 0) this condition implies that if the given numbers by our user is less that zero then the values to be displayed will be negative numbers.

I hope you will find my sample program beneficial and useful in your learning how to use arrays as your data structure in your C++ program. If you have some questions about my work please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

Thank you very much.


Sample Output of Our Program

Program Listing

#include <iostream>

using namespace std;

main()
{
    int numbers[5];
    cout << "\n\n";
    cout << "\t Positive and Negative Numbers Counter";
    cout << "\n\n";
    for (int x=0; x<5; x++) {
        cout << "Enter item no." << x+1 <<  ": ";
        cin >> numbers[x];
     }
 cout << "\n\n";
 cout << "Positive Numbers";
 cout << "\n";
  for (int y=0; y<5; y++) {
    if (numbers[y] >=0) {
        cout << " " << numbers[y] << "";
    }
  }
  cout << "\n\n";
  cout << "Negative Numbers";
 cout << "\n";
  for (int z=0; z<5; z++) {
    if (numbers[z] <0) {
        cout << " " << numbers[z] << "";
    }
  }
 cout << "\n\n";
 cout << "\t Thank You For Using This Software.";
 cout << "\n\n";
}