Thread: me can't think

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    87

    me can't think

    Say you had a vector with some +ve ints and some -ve ints, how do you return the one which is closest to 0???
    PuterPaul.co.uk - Portfolio site

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    What's a +ve and -ve int?

    vector meaning std::vector, or vector meaning those things in physics?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Code:
    #define ABS(x) ((x) < 0 ? -(x) : (x))
    
    int GetClosestToZero(void)
    {
        int closest = myVector[0];
        for(int i = 1; i < myVector.size(); i++)
        {
            if(ABS(myVector[i]) < ABS(closest))
            {
                closest = myVector[i];
            }
        }
        return(closest);
    }
    I haven't compiled this code, it's designed to give you an idea of the kind of thing you should be doing. It assumes there is at least one value in myVector.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    +ve = positive
    -ve = negative
    PuterPaul.co.uk - Portfolio site

  5. #5
    Registered User rahaydenuk's Avatar
    Join Date
    Jul 2002
    Posts
    69

    Re: me can't think

    Originally posted by pdstatha
    Say you had a vector with some +ve ints and some -ve ints, how do you return the one which is closest to 0???
    As someone has already asked, can you please clarify what you mean by 'vector'?

    Do you mean the mathematical construct (a one dimensional matrix) or do you mean std::vector??
    Richard Hayden. ([email protected])
    Webmaster: http://www.dx-dev.com
    DXOS (My Operating System): http://www.dx-dev.com/dxos

    PGP: 0x779D0625

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    std::vector that one
    PuterPaul.co.uk - Portfolio site

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    16
    what do you mean by +ve and -ve ?

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    +ve = POSITIVE
    -ve = NEGATIVE

    or just read previous thread
    PuterPaul.co.uk - Portfolio site

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Look at Uraldor's code.

Popular pages Recent additions subscribe to a feed