Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Array Members Counter

array

  • Please log in to reply
3 replies to this topic

#1 Egz0N

Egz0N

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1155 posts

Posted 20 December 2009 - 06:24 AM

Hello CC Members ..:D

Today I'd Like to Show You How to Make a Simple Program that will Count and tell us How many Positive and Negative Members are there in an Array ..

So,

#include <iostream.h>
int main()
{
    const int m=7, A[m]={6,-2,-9,4,7,3,-5};
    int i,p,n;

First, We Declare the Variables ..
We Declare and Initialize an Array with 7 members on it ..The Members are {6,-2,-9,4,7,3,-5} .. Our Counter will Count these Members and Will Tell us How Many of them are Positive and How Many Negative .. We also declare the Variables "i,p,n" .. (p for positive members and n for negative members)
p=0;
n=0;
    
    for (i=0;i<m;i++)
            if (A[i]<0)
                 n=n+1;
            else
                 p=p+1;

We Initialize the "p" and the "n" 0 .. Because the Counter will Start to Count from 0 ..

Now, We Make a Loop that Will go Through each Array Member Starting from 0 (A0 = the first Array Member) till m [m=7 (the last member A6)] increasing the "i" for one (i++) ..
The Counter Will Look at each Member so, if the Member is Smaller than 0 (that means the Member is a Negative Number .. so the "n" Counter will increase for 1 ..) or if it isn't, the "p" Counter will increase for 1 ..

The Loop Will Continue to Count till the Last Array Member .. and When it Comes to the Last One .. it Will Tell us How Many Positive and Negative Members are there..

..

cout << "\nPositive Array Members p="
         << p
         << "\n";
    cout << "\nNegative Array Members n="
         << n
         << "\n\n";

Now, The Part Where we print "p" and "n" ..
"\n"
means New Line .. so the output in the console application will start from the second line .. than after printing "p" there's another New Line and than the the "n" will be printed too ..

..

system ("pause");
         
         return 0;
         }

Now, We add a
system("pause")
to Make the Console Application Window stay up .. otherwise if we don't add this Part in our Code.. the Console Application Window will just appear and than disappear again ..

..

Finally the Code will Look Like the Following:


#include <iostream.h>
int main()
{
    const int m=7, A[m]={6,-2,-9,4,7,3,-5};
    int i,p,n;
    p=0;
    n=0;
    
    for (i=0;i<m;i++)
            if (A[i]<0)
                 n=n+1;
            else
                 p=p+1;
                 
    cout << "\nPositive Array Members p="
         << p
         << "\n";
    cout << "\nNegative Array Members n="
         << n
         << "\n\n";
         
         system ("pause");
         
         return 0;
         }

And the Console Application:

http://forum.codecal...=1&d=1261482680


Thanks,

Egz0N..

Attached Thumbnails

  • AMC.JPG

Attached Files


Edited by Egz0N, 22 December 2009 - 03:55 AM.

  • 1

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 21 December 2009 - 08:28 AM

Given that A is an array, not a vector, isn't the title a little misleading?
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Egz0N

Egz0N

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1155 posts

Posted 22 December 2009 - 04:01 AM

Given that A is an array, not a vector, isn't the title a little misleading?


:o Since I Made the Tutorial First in Albanian (my Language) and than I tried to adapt it on the best way into English .. I forgot to translate it into English [I mean the word Vector (into Albanian "Vektor")] .. but now it's Fixed .. Thanks ..:)
  • 0

#4 bekace

bekace

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 06 January 2010 - 03:58 PM

:o Since I Made the Tutorial First in Albanian (my Language) and than I tried to adapt it on the best way into English .. I forgot to translate it into English [I mean the word Vector (into Albanian "Vektor")] .. but now it's Fixed .. Thanks ..:)


yeap
  • 0





Also tagged with one or more of these keywords: array

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download