Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Xcode] Absolute value issue in C

  1. Oct 3, 2013 #1
    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
     
  2. jcsd
  3. Oct 3, 2013 #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
     
  4. Oct 3, 2013 #3
    I'll try it out when I get out of class.

    Thanks, Gary
     
  5. Oct 3, 2013 #4

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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...
     
  6. Oct 3, 2013 #5

    Mark44

    Staff: Mentor

    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.

    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 (Text):
    #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;
    }
     
     
  7. Oct 3, 2013 #6
    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.
     
  8. Oct 3, 2013 #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 (Text):
     
    #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;
       
        }
     
     
  9. Oct 3, 2013 #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:

    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 (Text):
    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
    }
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Xcode] Absolute value issue in C
Loading...