+ Reply to Thread
Results 1 to 4 of 4

Thread: Array Members Counter

  1. #1
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Smile Array Members Counter

    Hello CC Members ..

    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,

    Code:
    #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)
    Code:
    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..

    ..

    Code:
    cout << "\nPositive Array Members p="
             << p
             << "\n";
        cout << "\nNegative Array Members n="
             << n
             << "\n\n";
    Now, The Part Where we print "p" and "n" ..
    Code:
    "\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 ..

    ..

    Code:
             system ("pause");
             
             return 0;
             }
    Now, We add a
    Code:
    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:


    Code:
    #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:




    Thanks,

    Egz0N..
    Attached Thumbnails Attached Thumbnails Array Members Counter-amc.jpg  
    Attached Files Attached Files
    Last edited by Egz0N; 12-22-2009 at 03:55 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Vector Members Counter

    Given that A is an array, not a vector, isn't the title a little misleading?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Re: Array Members Counter

    Quote Originally Posted by WingedPanther View Post
    Given that A is an array, not a vector, isn't the title a little misleading?
    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 ..

  5. #4
    bekace is offline Newbie
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Re: Array Members Counter

    Quote Originally Posted by Egz0N View Post
    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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Greetings to all members..
    By Rohan in forum Introductions
    Replies: 6
    Last Post: 11-02-2010, 09:35 AM
  2. Search for members
    By Bioshox in forum PHP Development
    Replies: 1
    Last Post: 04-27-2010, 05:19 AM
  3. By Members for Members - IRC Channel
    By BlaineSch in forum Member Software/Projects
    Replies: 25
    Last Post: 03-26-2010, 10:20 AM
  4. Initialization List Array Members
    By Buttacup in forum C and C++
    Replies: 8
    Last Post: 01-03-2010, 08:58 PM
  5. Hello Members of CodeCall
    By whitey6993 in forum Introductions
    Replies: 6
    Last Post: 12-26-2008, 09:14 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts