Jump to content
1 minute ago, James Evens said:

Read the documentation on floating points variables/types. For this homework the part describing how a float works (especially how the negative values work)

 

no documentation in my  question I only got this: Write program receiving an integer and prints the absolute value (absolute value).
Do not use the abs command except for conditions only! Please note that in this question only one condition is sufficient.

Link to comment
https://linustechtips.com/topic/1276752-c-abs-value/#findComment-14269824
Share on other sites

Link to post
Share on other sites

You scan the number and you put it into a variable.

 

You can then use IF THEN ELSE with that variable

 

if variable < 0 then variable = -1 * variable;   <-- change this to c syntax

 

another option is to use bit operations but this is probably above what you know right now : https://en.wikipedia.org/wiki/Two's_complement

 

basically , integer number are stored as a number of bytes, and each byte has 8 bits

A negative number has the first bit set to 1, so to convert sign you just flip all bits and then add 1 ... see first 2-3 answers : https://stackoverflow.com/questions/7809864/how-to-convert-negative-number-to-positive-by-operator-in-c

 

Link to comment
https://linustechtips.com/topic/1276752-c-abs-value/#findComment-14271044
Share on other sites

Link to post
Share on other sites

8 hours ago, Liam mallka said:

I know how to scan ab integer but im not sure how to check but I think that it goes like that "if (x < 0)

Post what you have and people can help.  As other's have said, you actually are able to use an if statement.  The general logic being

if value read < 0 then value read = -value read

[continue with your code]

3735928559 - Beware of the dead beef

Link to comment
https://linustechtips.com/topic/1276752-c-abs-value/#findComment-14272681
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×