Thread: Calculation error

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    17

    Calculation error

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    void main(void)
    {
        //declaration
        float x, y, distance, slope;
    
        //intialization
        printf("Enter the co-ordinates of point P (X , Y): ");
        scanf("%f %f", &x, &y);
        printf("\nThe point is ( %f , %f )", x, y);
    
        //calculation
        
        slope = y / x;
        distance = sqrt( x * x + y * y);
        
    
        if ( x = 0 )
        {
            printf("The slope is infinity!"); 
            printf("The distance is %.2f", distance);
        }
    
        else
        {
            printf("\nThe slope is %.2f and the distance is %.2f",slope, distance);
    
        }
        exit( 0 );
    
    } /* end main program */
    Problem with the infinity part , the output shows 1.#J for some reason and I don't understand which part went wrong.

    If X and Y are both numbers , the program seems to be working fine but if X = 0 , the problem just shows the slope output to be 1.#J instead of showing " The slope is infinity"
    Last edited by Muppetlol; 05-17-2012 at 11:47 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Manitoba, Canada
    Posts
    12
    Code:
    if ( x = 0 )
    This should be:

    Code:
    if(x == 0)
    Remember "=" is for assignment, it's setting 'x' to zero, == is for comparisons.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You calculate slope before you check if x is 0. You should not be dividing by 0. You should only be calculating the slope after you've verified that x is not 0. This:
    Code:
    //calculation
         
    slope = y / x;
    distance = sqrt( x * x + y * y);
         
     
    if ( x == 0 )
    {
        printf("The slope is infinity!");
        printf("The distance is %.2f", distance);
    }
    else
    {
        printf("\nThe slope is %.2f and the distance is %.2f",slope, distance);
    }
    Should be this:
    Code:
    //calculation
         
    distance = sqrt( x * x + y * y);
         
    if ( x == 0 )
    {
        printf("The slope is infinity!");
        printf("The distance is %.2f", distance);
    }
    else
    {
        slope = y / x;
        printf("\nThe slope is %.2f and the distance is %.2f",slope, distance);
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by Kazansky View Post
    Remember "=" is for assignment, it's setting 'x' to zero, == is for comparisons.
    As a corollary to this: COMPILE WITH WARNINGS ENABLED, and the compiler would have TOLD you this before it even finished compiling.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array calculation error need help
    By Serj in forum C Programming
    Replies: 6
    Last Post: 12-28-2011, 07:42 PM
  2. Please Help ASAP!!! Calculation error!
    By JSM in forum C Programming
    Replies: 3
    Last Post: 11-08-2010, 05:46 AM
  3. Average Calculation Error
    By BamaMarine06 in forum C Programming
    Replies: 2
    Last Post: 10-04-2010, 08:13 PM
  4. source of calculation error?
    By stanlvw in forum C++ Programming
    Replies: 11
    Last Post: 08-12-2004, 05:04 PM
  5. Arrays/calculation error... *need Help*
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 12:45 AM