help with a number counting function

This is a discussion on help with a number counting function within the C Programming forums, part of the General Programming Boards category; in the program, there is a section where you generate a series of numbers that is seeded from a user's ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    16

    Talking help with a number counting function

    in the program, there is a section where you generate a series of numbers that is seeded from a user's input. the user also determines how many numbers should be generated. this part is fine, however, it also has to count the number of positives, negatives, zeros, odds, and evens and print them to screen. plz helllllp

    Code:
    void genRandom(int seed, int lb, int ub, int numToGen,
                   int* pEven, int* pOdd, int* pNeg, int* pPos, int* pZero)
    {   
       int i;
       int randNum;
             
       srand(seed);
       printf("\nNumbers generated:");
       printf("\n");
       for (i = 0 ; i < numToGen; i++)
       {
          randNum = rand() % (ub - lb + 1) + lb;
          printf("%4d", randNum);
       }
       addToCounts(randNum, pEven, pOdd, pNeg, pPos, pZero);
    
       printf("\n\n");
       return;
    }  
       
    void addToCounts(int randNum, int* pEven, int* pOdd,
                     int* pNeg, int* pPos, int* pZero)
    {
       int i;
     
       for (i = 0; randNum > 0; i++)        
            pPos++;
    }

    the addToCounts function is the one where i have no idea what to do. i only have pos in there to try to figure it out but i intend to include all the rest. thank you for your time
    Last edited by laserlight; 03-10-2009 at 06:31 AM. Reason: Fixed code tags.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The variables go to addToCounts will be pointers. They should be set to zero before the numbers are generated.

    If statements will sort out the counting of pos, neg, etc.

    Code:
    if(randNum > 0)
       *pos++;
    else if(randNum < 0)
       *neg++;
    
    if(randNum % 2 == 0)
    well, you should figure out the rest.

    Get some indentation going on that program! It will help you find your logic and syntax errors, easier.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    If you take a look at the operator precedence http://www.decom.ufop.br/prof/romild...c-op-prec.html

    you'll see that
    *pos++;

    is equivalent to
    *(pos++);

    I do not see what it can be good for
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    Obviously Adak just meant ++*pos and ++*neg, or (*pos)++ and (*neg)++. As for what the *pos++ syntax can be good for, I suppose the canonical strcpy() implementation is one example:
    Code:
    while (*dest++ = *src++);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 01:42 PM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 09:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21