Hi everyone,
Is there any specific technique to turn all of the array values from positive to negative?
Hi everyone,
Is there any specific technique to turn all of the array values from positive to negative?
A for loop with a[i] = -a[i]; perhaps?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
If they are all non-negative, just set to the negative, as Salem suggests. Note that in C, = means "assign".
If some are negative, and you want them all to be negative, then you need to test them first, then sign flip the positive ones.
Note that because of how binary numbers work, INT_MIN is one lower than -INT_MAX. This won't affect you as you are converting from positive to negative. However if you do the reverse conversion you need to test for INT_MIN and flag an error.
I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
Visit my website for lots of associated C programming resources.
https://github.com/MalcolmMcLean