Thread: Why can't my program subtract a negative number from a positive one?

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    3

    Why can't my program subtract a negative number from a positive one?

    My program uses a while loop to eventually get to an error of zero and a root of sqrt(3). I'm not understand why after the third iteration the program fails to compute a new x value. I'm using Visual Studio 2013. Your help is greatly appreciated!The code tag instructions were dubious, so forgive me on this first post.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    main()
    {
    
        /*This program uses the Newton-Raphson method to solve y = (x^3)-3 for it's roots.*/
        printf("This program uses the Newton-Raphson method to solve y = (x^3)-3 for it's roots. Enter your estimate of the root.\n");
        float x,y,z;
        int num;
        num = 0;
        /*User inputs an initial estimate of root (around 1.6)*/
        scanf_s("%f", &x);
    
    
        /*Program prints initial estimate*/
        printf("initial x = %f\n", x);
        /*If error is greater than .000001, then program may continue*/
        y = abs(((x - sqrt(3)) / x)*100);
        z = 3 * pow(x, 2);
        /*If derivative is not equal to zero and the number of iterations has not exceed 500, then the program may continue*/
            while (y >= .000001, z != 0, num <= 499)
    {
             
                num++;
                x = x - ((pow(x, 3)-3) / (3 * pow(x, 2)));
                printf("number of iterations:%d\n", num);
                printf("estimate of root: x = %f\n", x);
                y = abs(((x - sqrt(3)) / x)*100);
                printf("value of error = %f\n", y);
                system("pause");
                z = 3 * pow(x, 2);
    }
             
    /*If the initial estimate produces an error less than or equal to .000001, then the program states the initial guess and stops the program from running*/
        printf("estimate of root: x = %f/n", x);
        system("pause");
        return 0;}
    Attached Images Attached Images Why can't my program subtract a negative number from a positive one?-capture-png 

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Compile your code at the maximum warning level (-Wall for gcc, else check your compiler documentation or Google it). Here's what I get
    Code:
    $ make foo
    gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c:4:1: warning: return type defaults to ‘int’ [enabled by default]
     main()
     ^
    foo.c: In function ‘main’:
    foo.c:13:5: warning: implicit declaration of function ‘scanf_s’ [-Wimplicit-function-declaration]
         scanf_s("%f", &x);
         ^
    foo.c:19:5: warning: implicit declaration of function ‘abs’ [-Wimplicit-function-declaration]
         y = abs(((x - sqrt(3)) / x)*100);
         ^
    foo.c:22:24: warning: left-hand operand of comma expression has no effect [-Wunused-value]
         while (y >= .000001, z != 0, num <= 499)
                            ^
    foo.c:22:32: warning: left-hand operand of comma expression has no effect [-Wunused-value]
         while (y >= .000001, z != 0, num <= 499)
                                    ^
    foo.c:31:9: warning: implicit declaration of function ‘system’ [-Wimplicit-function-declaration]
             system("pause");
             ^
    /tmp/cctruZQG.o: In function `main':
    /usr/local/google/home/cagarvin/sandbox/cprogramming/foo.c:13: undefined reference to `scanf_s'
    collect2: error: ld returned 1 exit status
    make: *** [foo] Error 1
    • Line 4: Explicitly declare main to return an int, don't make the compiler guess
    • Line 13: scanf_s is a non-standard function, you code likely wont work on Linux or other non-Windows. This is probably fine for you, but you should be aware.
    • Line 19: abs() is weird, it's a math function, but it's in stdlib.h.
    • Line 22: That does not do what you think. You do not use a comma to combine several conditional statements. You need to use logical and (&&) and or (||) operators.
    • Line 31: You need to #include <stdlib.h> to use system(). Also, "pause" is Windows-specific. Consider: FAQ > Stop my Windows Console from disappearing everytime I run my program? - Cprogramming.com
    • It's conventional to use < X instead of <= X-1 for loops that count, that way you use the actual limit in the expression instead of one less than the limit. So <= 499 should be < 500, but...
    • Don't use magic numbers. Define constants with descriptive names and use them. .000001 could be called ESTIMATE_TOLERANCE and 500 could be called MAX_ITERATIONS.
    • You use a /n (forward slash) instead of \n (backslash) in at least one place.


    Just so you know, the comma operator evaluates all of the operands, and returns the value of the right-most operand. Thus, your loop condition is equivalent to: "check if y >= .000001, then throw that result away; check if z != 0, then throw that result away; check if num <= 499 and use that for the while loop condition". A simpler example may be:
    Code:
    int x = 1, 2, 3;  // assigns x the value of 3, 1 and 2 are basically ignored
    The comma operator does find uses though, especially when some of the operands have side effects like modifying a variable or calling a function. The most common place I see it is in compound for loop initializers and increments.

    Also note, the comma operator, as shown above, is not the same thing as the commas used to separate function parameters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-29-2012, 05:11 AM
  2. count the number of positive and negative numbers
    By Arex Bawrin in forum C Programming
    Replies: 5
    Last Post: 04-04-2011, 07:24 PM
  3. Negative Float Into Positive
    By Cero.Uno in forum C Programming
    Replies: 2
    Last Post: 02-17-2008, 03:13 AM
  4. How can i convert negative number to positive number ?
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 05-05-2004, 08:02 AM
  5. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM

Tags for this Thread