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

Printable View

- 08-04-2002pdstathame 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???

- 08-04-2002SilentStrike
What's a +ve and -ve int?

vector meaning std::vector, or vector meaning those things in physics? - 08-04-2002UraldorCode:
`#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);

}

U. - 08-05-2002pdstatha
+ve = positive

-ve = negative - 08-05-2002rahaydenukRe: me can't thinkQuote:

*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???**

Do you mean the mathematical construct (a one dimensional matrix) or do you mean std::vector?? - 08-05-2002pdstatha
std::vector that one

- 08-05-2002aym_7
what do you mean by +ve and -ve ?

- 08-05-2002pdstatha
+ve = POSITIVE

-ve = NEGATIVE

or just read previous thread - 08-05-2002golfinguy4
Look at Uraldor's code.