Jump to content

C abs value

Liam mallka

Hey there, I want to  print absolute value in  c but I cannot use abs only conditions like if, else, else if, please help me out with that. :)

 

Link to comment
Share on other sites

Link to post
Share on other sites

You should check if the value is lower than zero and if it is just return the negative of that value.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Sauron said:

You should check if the value is lower than zero and if it is just return the negative of that value.

Im scanning the number from the client so I cant do that.

Link to comment
Share on other sites

Link to post
Share on other sites

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
Share on other sites

Link to post
Share on other sites

23 minutes ago, Liam mallka said:

Im scanning the number from the client so I cant do that.

What? Why not?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Sauron said:

What? Why not?

I dont know how XD

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Liam mallka said:

I dont know how XD

You don't know how to check if an integer is less than zero? Or how to scan an integer from input?

Link to comment
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
Share on other sites

Link to post
Share on other sites

float abs_value(float f) { 
	return f < 0 ? -f : f; 
}

or you could just use this expression:

f < 0 ? -f : f

 

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, badreg said:

You don't know how to check if an integer is less than zero? Or how to scan an integer from input?

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)

Link to comment
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
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

×