Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Absolute Value In C
htdefiant
post Sep 1 2007, 09:54 PM
Post #1


Advanced Member
*******

Group: Members
Posts: 125
Joined: 21-February 06
Member No.: 18,973



Hey,
I'm writing a program for a friend in Calculus. The goal is for him to be able to input a number, and have the computer return whether it is positive or negative, and its absolute value. Here is what I have so far:
CODE

/* positive.cpp
* The program that gets a number and checks if it is a positive number.
*It prints an appropriate message indicating if it’s positive or negative and also prints the
*absolute value of the number.
*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

main ()

{
int num1;
bool positive;
printf("This program will determine if the user inputted number is positive or negative. \n");
printf("It will also display the number's absolute value. \n");
printf("Please enter a number: \n");
num1=GetInteger ();
if (num1==0)
printf("The number %d is neither positive nor negative. \n", num1);
else
if (num1>0)
{
printf("The number %d is positive and its absolute value is %d. \n", num1, num1);
positive=true;
}
else
{
printf("The number %d is negative and its absolute value is %d. \n", num1, num1);
positive=false;
}
getchar ();
return 0;
}


Obviously the line
CODE
printf("The number %d is negative and its absolute value is %d. \n", num1, num1);

is flawed. How do I fix it so if the number is negative it will display the correct absolute value?

Thank you.
Go to the top of the page
 
+Quote Post
Tetraca
post Sep 1 2007, 10:36 PM
Post #2


Privileged Member
*********

Group: Members
Posts: 628
Joined: 20-May 06
Member No.: 23,968



A function would be what you are looking for. I'm sure there's one in the standard library for it called abs which will change it to an absolute number.
Go to the top of the page
 
+Quote Post
htdefiant
post Sep 2 2007, 02:07 AM
Post #3


Advanced Member
*******

Group: Members
Posts: 125
Joined: 21-February 06
Member No.: 18,973



So what would the line be?
Yes, there is a function called abs.

Something like this:
printf("The number %d is negative and its absolute value is %d. \n", num1, abs:num1);
?
Go to the top of the page
 
+Quote Post
fffanatics
post Sep 2 2007, 10:22 PM
Post #4


Privileged Member
*********

Group: [HOSTED]
Posts: 936
Joined: 14-April 05
From: West Chester, PA
Member No.: 5,636



The correct syntax for that line would be:

printf("The number %d is negative and its absolute value is %d. \n", num1, abs(num1));

also if you wanted to get the absolute value of a floating point number instead you can use fabs(variable).
Go to the top of the page
 
+Quote Post
htdefiant
post Sep 3 2007, 03:50 PM
Post #5


Advanced Member
*******

Group: Members
Posts: 125
Joined: 21-February 06
Member No.: 18,973



Thanks, it worked great.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Server Absolute Referencing(4)
  2. Those Absolute Essentials(0)
  3. Podcasting For The Absolute Newb(5)
  4. Free Web Hosting: (absolute)(1)
  5. What Is Absolute Zero?(9)
  6. Free Web Hosting: (absolute)(1)
  7. A Demonstration Of Floats, Clears And Absolute Positions(1)
  8. Free Web Hosting: (absolute Zer0) [screened] [approved](2)


 



- Lo-Fi Version Time is now: 22nd November 2008 - 02:19 PM