# [Xcode] Absolute value issue in C

## Main Question or Discussion Point

Hey PF,

I'm building a simple math calculator program in C for my Programming class. My problem is Xcode doesn't recognize the absolute value function (abs), even with math.h included:

Any ideas on what I can do to fix this? Is there a way for me to define this on my own?

Thank you,

fysika

Related Programming and Computer Science News on Phys.org
fysika,

I just made a quick draft in order to get it to work.
Use this and apply it to your code.

#include <stdio.h>
#include <string.h>
#include <math.h>

int main(void)
{

int num = 0;

printf("Enter number\n");
scanf("%d", &num);

printf("%d is the absolute value", answer);

return 0;
}

That worked fine for me. If you post your entire code, I can take a look at it and see if there is anything missing or throwing it off.

Hope this helps!

-Gary Alfonso

I'll try it out when I get out of class.

Thanks, Gary

AlephZero
Homework Helper
doesn't recognize the absolute value function (abs), even with math.h included:
That's because abs() is declared in stdlib.h, not math.h

(IMO MrTheBard seems to be using a non-standard C compiler, if his code worked.)

But be careful with the abs() function, because in C it only works on integers! Maybe you really wanted fabs(), which is in math.h.

The code in your screenshot doesn't include any variable declarations, but a calculator with a square root function, but which only works on integers, seems a bit strange...

Mark44
Mentor
Hey PF,

I'm building a simple math calculator program in C for my Programming class. My problem is Xcode doesn't recognize the absolute value function (abs), even with math.h included:

Any ideas on what I can do to fix this? Is there a way for me to define this on my own?

Thank you,

fysika
When you post code, it's better to copy and paste the code into this window, rather than post a screen shot. After all, your code is text. If you post the code rather than an image of it, people who respond can insert comments at the appropriate places in your code. They can't do that with an image.

fysika,

I just made a quick draft in order to get it to work.
Use this and apply it to your code.

#include <stdio.h>
#include <string.h>
#include <math.h>

int main(void)
{

int num = 0;

printf("Enter number\n");
scanf("%d", &num);

printf("%d is the absolute value", answer);

return 0;
}

That worked fine for me. If you post your entire code, I can take a look at it and see if there is anything missing or throwing it off.

Hope this helps!

-Gary Alfonso
When you post code, it's considered good manners to use HTML "code" tags, which preserve indentation and help make the code more readable. Put a [code] tag at the top and a [/code] tag at the bottom.

Here's what yours looks like with code tags:
Code:
#include <stdio.h>
#include <string.h>
#include <math.h>

int main(void)
{

int num = 0;

printf("Enter number\n");
scanf("%d", &num);

printf("%d is the absolute value", answer);

return 0;
}

That's because abs() is declared in stdlib.h, not math.h

(IMO MrTheBard seems to be using a non-standard C compiler, if his code worked.)

But be careful with the abs() function, because in C it only works on integers! Maybe you really wanted fabs(), which is in math.h.

The code in your screenshot doesn't include any variable declarations, but a calculator with a square root function, but which only works on integers, seems a bit strange...
My professor only mentioned math.h. I just cropped it to show the error, give me a second and I'll post the entire code: I have one answer declared as an integer and another one declared as a float.

Additionally, any idea on what I can do for division? When the denominator is 0 I'm supposed to have it give an error and return to menu. Right now in between the issued error and the menu return, the program still returns the (nonsensical) answer.

Code:
#include <stdio.h>

#include <math.h>

int main(void) {

while (selection != 8) {

switch (selection) {

case 1: printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter a second number: ");

scanf("%d", &num2);

printf("%d + %d = %d\n", num1, num2, answer);

break;

case 2: printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter a second number: ");

scanf("%d", &num2);

printf("%d - %d = %d\n", num1, num2, answer);

break;

case 3: printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter a second number: ");

scanf("%d", &num2);

printf("%d * %d = %d\n", num1, num2, answer);

break;

case 4: printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter a second number: ");

scanf("%d", &num2);

while (num2==0) {

printf("ERROR: You can not divide by zero.\n");

printf("%d / %d = %.2f\n", num1, num2, answer2);

break;

case 5: printf("Enter a number: ");

scanf("%d", &num1);

printf("The absolute value of %d is %d", num1, answer);

break;

case 6: printf("Enter a number: ");

scanf("%d", &num1);

if (num1<0) {

printf("ERROR: You can not take the square root of a negative number with this program.");
}

printf("The sqrt %d is %.2f", num1, answer2);

break;

case 7: printf("Enter a base number: ");

scanf("%d", &num1);

printf("Enter an exponent: ");

scanf("%d", &num2);

while ((num1==0) && (num2<=0)) {

printf("You can not raise zero to the zeroth power or to a negative number\n");

printf("Enter another exponent: ");

scanf("%d", &num2);
}

printf("%d ^ %d = %d\n", num1, num2, answer);

break;

default: printf("%d is not a valid selection\n", selection);

}

}

printf("Bye!\n");

}

int choice;

printf("2: Subtract\n");

printf("3: Multiply\n");

printf("4: Divide\n");

printf("5: Absolute value\n");

printf("6: Square Root\n");

printf("7: Powers\n");

printf("8: Quit\n");

printf("\n");

printf("Enter a selection: ");

scanf("%d", &choice);

printf("\n");

return choice;

}

Let us look at one part of your code, and you should get how to handle the error condition for division once you know how to structure this code:

Code:
            case 6: printf("Enter a number: ");

scanf("%d", &num1);

if (num1<0) {

printf("ERROR: You can not take the square root of a negative number with this program.");
}

printf("The sqrt %d is %.2f", num1, answer2);

break;
First off, your indentation and formatting are really bad, and you need to improve that for many reasons, but for one, I find it hard to read quickly and accurately. Doubt it compiles like that with an apparently unterminated while loop in your division section. That aside, think about this: You have two branches of logic you wish to capture: 1. Inputs are bad, tell user, and 2. Inputs are ok, do the operation and print result.

Reflect that in your code. Maybe you wish to use a loop to re-ask the number until it is correct, but aside from that, you have your if statement checking to see if the number was acceptable. If it is, that's one logic branch where you do your thing. If not, then that's the other where you do not run the operation, but you tell the user what the problem is. Use if (expression) { ... } else { ... } type constucts.

The same works for your divide by 0. Basically:
Code:
If (denominator is not 0)
{
// divide and print results, knowing a 0 denominator is impossible here.
}
else
{
// print error message because division by 0 is undefined
}