[Xcode] Absolute value issue in C

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.
  • #1
12
0
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:

Rt74QOh.png


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
 
Technology news on Phys.org
  • #2
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;
int answer = 0;

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

answer = abs(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
 
  • #3
I'll try it out when I get out of class.

Thanks, Gary
 
  • #4
fysika said:
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...
 
  • #5
fysika said:
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.

MrTheBard said:
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;
int answer = 0;

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

answer = abs(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;
    int answer = 0;
    
    printf("Enter number\n");
    scanf("%d", &num);
    
    answer = abs(num);
    printf("%d is the absolute value", answer);
   
   return 0;
}
 
  • #6
AlephZero said:
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.
 
  • #7
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) {
    
        int selection, num1, num2, answer;
    
        float answer2;
    
selection = menu ();
        
    while (selection != 8) {
        
        switch (selection) {
                
            case 1: printf("Enter first number: ");
                
                    scanf("%d", &num1);
                
                    printf("Enter a second number: ");
                
                    scanf("%d", &num2);
            
               
                answer = num1 + 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);
                
                answer = num1 - 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);
        
                answer = num1 * 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);
                
                                
                
                answer2 = (float)num1 / 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);
                
                answer = abs(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.");
                        }
                
                answer2 = sqrt(num1);
                
                    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);
                        }

                
                answer = pow(num1, num2);
                
                
                    printf("%d ^ %d = %d\n", num1, num2, answer);
                
                
                break;
                
               
            
        default: printf("%d is not a valid selection\n", selection);                
                
                
        }
        
        selection = menu ();
        
    }

    printf("Bye!\n");
    
}


    int menu(selection) {
    
    int choice;
    
        printf("1: Add\n");
    
        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;
    
    }
 
  • #8
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:

fysika said:
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.");
                        }
                
                answer2 = sqrt(num1);
                
                    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
}
 

1. What is the absolute value function in C?

The absolute value function in C is a mathematical function that returns the positive value of a given number. It essentially removes the negative sign from a negative number, and leaves a positive number unchanged. In C, the absolute value function is represented by the abs() function.

2. How do I use the absolute value function in my Xcode project?

To use the absolute value function in your Xcode project, you will need to include the stdlib.h header file in your code. This header file contains the declaration for the abs() function. Then, you can simply call the abs() function and pass in the number you want to find the absolute value of.

3. Can I find the absolute value of a variable in C?

Yes, you can find the absolute value of a variable in C. You can use the abs() function to find the absolute value of any numerical variable, whether it is an integer or a floating-point number. Just make sure to include the stdlib.h header file in your code first.

4. What happens if I pass a negative number to the absolute value function in C?

If you pass a negative number to the absolute value function in C, the function will return the positive value of that number. For example, if you pass in -5, the function will return 5. If you pass in a positive number, the function will simply return that number unchanged.

5. Are there any other ways to find the absolute value of a number in C?

Yes, there are other ways to find the absolute value of a number in C. Another option is to use the fabs() function, which is specifically designed for floating-point numbers. This function is also included in the math.h header file. Additionally, you can use conditional statements to check if a number is negative and then multiply it by -1 to get its absolute value.

Suggested for: [Xcode] Absolute value issue in C

Replies
1
Views
865
Replies
29
Views
1K
Replies
22
Views
2K
Replies
9
Views
2K
Replies
3
Views
936
Replies
1
Views
2K
Replies
11
Views
1K
Back
Top