Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

C abs value

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

What is scaling and how does it work? Asus PB287Q unboxing! Console alternatives :D Watch Netflix with Kodi on Arch Linux Sharing folders over the internet using SSH Beginner's Guide To LTT (by iamdarkyoshi)

Sauron'stm Product Scores:

Spoiler

Just a list of my personal scores for some products, in no particular order, with brief comments. I just got the idea to do them so they aren't many for now :)

Don't take these as complete reviews or final truths - they are just my personal impressions on products I may or may not have used, summed up in a couple of sentences and a rough score. All scores take into account the unit's price and time of release, heavily so, therefore don't expect absolute performance to be reflected here.

 

-Lenovo Thinkpad X220 - [8/10]

Spoiler

A durable and reliable machine that is relatively lightweight, has all the hardware it needs to never feel sluggish and has a great IPS matte screen. Downsides are mostly due to its age, most notably the screen resolution of 1366x768 and usb 2.0 ports.

 

-Apple Macbook (2015) - [Garbage -/10]

Spoiler

From my perspective, this product has no redeeming factors given its price and the competition. It is underpowered, overpriced, impractical due to its single port and is made redundant even by Apple's own iPad pro line.

 

-OnePlus X - [7/10]

Spoiler

A good phone for the price. It does everything I (and most people) need without being sluggish and has no particularly bad flaws. The lack of recent software updates and relatively barebones feature kit (most notably the lack of 5GHz wifi, biometric sensors and backlight for the capacitive buttons) prevent it from being exceptional.

 

-Microsoft Surface Book 2 - [Garbage - -/10]

Spoiler

Overpriced and rushed, offers nothing notable compared to the competition, doesn't come with an adequate charger despite the premium price. Worse than the Macbook for not even offering the small plus sides of having macOS. Buy a Razer Blade if you want high performance in a (relatively) light package.

 

-Intel Core i7 2600/k - [9/10]

Spoiler

Quite possibly Intel's best product launch ever. It had all the bleeding edge features of the time, it came with a very significant performance improvement over its predecessor and it had a soldered heatspreader, allowing for efficient cooling and great overclocking. Even the "locked" version could be overclocked through the multiplier within (quite reasonable) limits.

 

-Apple iPad Pro - [5/10]

Spoiler

A pretty good product, sunk by its price (plus the extra cost of the physical keyboard and the pencil). Buy it if you don't mind the Apple tax and are looking for a very light office machine with an excellent digitizer. Particularly good for rich students. Bad for cheap tinkerers like myself.

 

 

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

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

 

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

What is scaling and how does it work? Asus PB287Q unboxing! Console alternatives :D Watch Netflix with Kodi on Arch Linux Sharing folders over the internet using SSH Beginner's Guide To LTT (by iamdarkyoshi)

Sauron'stm Product Scores:

Spoiler

Just a list of my personal scores for some products, in no particular order, with brief comments. I just got the idea to do them so they aren't many for now :)

Don't take these as complete reviews or final truths - they are just my personal impressions on products I may or may not have used, summed up in a couple of sentences and a rough score. All scores take into account the unit's price and time of release, heavily so, therefore don't expect absolute performance to be reflected here.

 

-Lenovo Thinkpad X220 - [8/10]

Spoiler

A durable and reliable machine that is relatively lightweight, has all the hardware it needs to never feel sluggish and has a great IPS matte screen. Downsides are mostly due to its age, most notably the screen resolution of 1366x768 and usb 2.0 ports.

 

-Apple Macbook (2015) - [Garbage -/10]

Spoiler

From my perspective, this product has no redeeming factors given its price and the competition. It is underpowered, overpriced, impractical due to its single port and is made redundant even by Apple's own iPad pro line.

 

-OnePlus X - [7/10]

Spoiler

A good phone for the price. It does everything I (and most people) need without being sluggish and has no particularly bad flaws. The lack of recent software updates and relatively barebones feature kit (most notably the lack of 5GHz wifi, biometric sensors and backlight for the capacitive buttons) prevent it from being exceptional.

 

-Microsoft Surface Book 2 - [Garbage - -/10]

Spoiler

Overpriced and rushed, offers nothing notable compared to the competition, doesn't come with an adequate charger despite the premium price. Worse than the Macbook for not even offering the small plus sides of having macOS. Buy a Razer Blade if you want high performance in a (relatively) light package.

 

-Intel Core i7 2600/k - [9/10]

Spoiler

Quite possibly Intel's best product launch ever. It had all the bleeding edge features of the time, it came with a very significant performance improvement over its predecessor and it had a soldered heatspreader, allowing for efficient cooling and great overclocking. Even the "locked" version could be overclocked through the multiplier within (quite reasonable) limits.

 

-Apple iPad Pro - [5/10]

Spoiler

A pretty good product, sunk by its price (plus the extra cost of the physical keyboard and the pencil). Buy it if you don't mind the Apple tax and are looking for a very light office machine with an excellent digitizer. Particularly good for rich students. Bad for cheap tinkerers like myself.

 

 

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

 

ಠ_ಠ  if i said something stupid please let me know

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

×