Distance Formula in my program..... I need help fast!!!

This is a discussion on Distance Formula in my program..... I need help fast!!! within the C Programming forums, part of the General Programming Boards category; I need help with using the distance formual in my program. I dont know the proper way to use the ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    11

    Unhappy Distance Formula in my program..... I need help fast!!!

    I need help with using the distance formual in my program. I dont know the proper way to use the square root function. I have figured out how too properly input the part of the distance formula that is inside the square root but i do not know how to take the square root of it. Here is my program:

    #include <stdio.h>

    int main (void)
    {
    /* X,Y = Coordinates of 1st Plane and A,B= Coordinates of 2nd
    Plane*/

    int integer; int X,Y,A,B;
    float distance;

    printf( "Enter the x and y coordinates of the first plane.\n");
    scanf( "%d %d", &X, &Y);

    printf( "Enter the x and y coordinates of the second plane.\n");
    scanf( "%d %d", &A, &B);

    distance= ((A-X)*(A-X))+((B-Y)*(B-Y));

    printf("The distance is %.2f\n", distance);

    return 0;

    }

    Do i float for distance since i have to include the sqaure root or do i use a double? Will some one please show me the correct way to execute the distance formula? I woule really appreciate it!!!!
    Last edited by Mackology101; 09-23-2004 at 07:55 PM.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Use the sqrt function in math.h. Here's the prototype:

    Code:
    double sqrt(double x);

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    11
    Where do i put the code?

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Look at this code.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void) {
         int integer, x1, y1, x2, y2;
         double distance;
    
         printf( "Enter the x and y coordinates of the first plane.\n");
         if(scanf( "%d %d", &x1, &y1) != 2)
              return -1;
         while(getchar() != '\n');
         printf( "Enter the x and y coordinates of the second plane.\n");
         if(scanf( "%d %d", &x2, &y2) != 2)
              return -1;
         while(getchar() != '\n');
         distance=sqrt((pow((x2-x1), 2))+(pow((y2-y1), 2)));
         printf("The distance is %.2f\n", distance);
         return 0;
    }
    When you compile, be sure to link with the math library (m).
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. Segfaulting Distance Program
    By radiohead in forum C Programming
    Replies: 2
    Last Post: 01-09-2006, 07:48 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21