Thread: help with a number counting function

  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 05: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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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++);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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-16-2006, 11:35 PM
  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, 12:42 PM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM