Thread: please tell me how to rectify my code

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, I think the correct formula is sqrt(|x1 - x2|^2 + |y1 - y2|^2), i.e.,
    Code:
    const double latitude_dist = p1.latitude - p2.latitude;
    const double longitude_dist = p1.longitude - p2.longitude;
    distance = sqrt(latitude_dist * latitude_dist + longitude_dist * longitude_dist);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Oops...
    Devoted my life to programming...

  3. #18
    Registered User
    Join Date
    Dec 2010
    Posts
    29

    Unhappy

    Code:
    #include <stdio.h>
    #include <math.h> 
    
    struct position{
    	double latitude;
    	double longitude;
    };
    
    
    
    int  main(void)
    {	
    	double distance;
    	struct position p1,p2;
    	scanf( "%f, %f, %f, %f", &p1.latitude, &p1.longitude, &p2.latitude, &p2.longitude);
    	const double latitude_dist = p1.latitude - p2.latitude;
    	const double longitude_dist = p1.longitude - p2.longitude;
    	distance = sqrt(latitude_dist * latitude_dist + longitude_dist * longitude_dist);
    	printf(" %f", distance);
    }
    yet no luck. rebuilt, debugged and ran.
    can you please tell me if the above code works on your computer.
    than you for your patience and help.

  4. #19
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    scanf+double ==> %lf
    Devoted my life to programming...

  5. #20
    Registered User
    Join Date
    Dec 2010
    Posts
    29

    Smile

    Quote Originally Posted by Sipher View Post
    scanf+double ==> %lf
    sorry, still no help.
    i am a newbie i don't know what's wrong with my compiler. however, if the code works on your compiler at least i know i am on the right track.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try this:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    struct position
    {
        double latitude;
        double longitude;
    };
    
    double computeDistance(const struct position *p1, const struct position *p2)
    {
        const double latitude_dist = p1->latitude - p2->latitude;
        const double longitude_dist = p1->longitude - p2->longitude;
        return sqrt(latitude_dist * latitude_dist + longitude_dist * longitude_dist);
    }
    
    int main(void)
    {
        struct position p1, p2;
        if (scanf("%lf, %lf, %lf, %lf",
            &p1.latitude, &p1.longitude, &p2.latitude, &p2.longitude) == 4)
        {
            printf("Distance = %f\n", computeDistance(&p1, &p2));
        }
        else
        {
            puts("Input error");
        }
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Dec 2010
    Posts
    29
    input :
    1 2 3 4

    output : input error
    Last edited by ilikeapplepie; 12-20-2010 at 08:55 AM.

  8. #23
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Now how's that happening?! Maybe removing the commas in scanf?
    Devoted my life to programming...

  9. #24
    Registered User
    Join Date
    Dec 2010
    Posts
    29
    Quote Originally Posted by Sipher View Post
    Now how's that happening?! Maybe removing the commas in scanf?
    yes, that worked no error. ty vry much. but now i am still getting a big number even with someone else's code.
    something must be wrong with my compiler. i am really grateful for your help.

  10. #25
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Why don't you post the four values that you inputed and the result you got? Maybe you just think it's huge while it's ok.
    Devoted my life to programming...

  11. #26
    Registered User
    Join Date
    Dec 2010
    Posts
    29
    Quote Originally Posted by Sipher View Post
    Why don't you post the four values that you inputed and the result you got? Maybe you just think it's huge while it's ok.
    input
    1 2 3 4

    output
    1726716016253713000000000000000000000000.000000000

  12. #27
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    o_O O_O O_o ...
    Devoted my life to programming...

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ilikeapplepie
    now i am still getting a big number even with someone else's code.
    What is your current code?

    Quote Originally Posted by ilikeapplepie
    something must be wrong with my compiler.
    What is your compiler?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Registered User
    Join Date
    Dec 2010
    Posts
    29
    Quote Originally Posted by laserlight View Post
    What is your current code?

    What is your compiler?
    i copied and pasted your code. i was getting input error till. i removed commas from scanf then i got the big number.

    visual c++ 2008 edition.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    struct position
    {
        double latitude;
        double longitude;
    };
    
    double computeDistance(const struct position *p1, const struct position *p2)
    {
        const double latitude_dist = p1->latitude - p2->latitude;
        const double longitude_dist = p1->longitude - p2->longitude;
        return sqrt(latitude_dist * latitude_dist + longitude_dist * longitude_dist);
    }
    
    int main(void)
    {
        struct position p1, p2;
        if (scanf("%lf %lf %lf %lf",
            &p1.latitude, &p1.longitude, &p2.latitude, &p2.longitude) == 4)
        {
            printf("Distance = %f\n", computeDistance(&p1, &p2));
        }
        else
        {
            puts("Input error");
        }
        return 0;
    }
    and this is my own code. i was getting all errors till you helped but here too, big number.
    Code:
    #include <stdio.h>
    #include <math.h> 
    
    struct position{
    	double latitude;
    	double longitude;
    };
    
    
    
    int  main(void)
    {	
    	double distance_lat;
    	double distance_lon;
    	double distance;
    	struct position p1,p2;
    	scanf( "%f, %f, %f, %f", &p1.latitude, &p1.longitude, &p2.latitude, &p2.longitude);
    	distance_lat=p1.latitude-p2.latitude;
    	distance_lon=p1.longitude-p2.longitude;
    	distance = sqrt( (distance_lat * distance_lat) + (distance_lon * distance_lon));
    	printf(" %f", distance);
    }
    Last edited by ilikeapplepie; 12-20-2010 at 09:20 AM.

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And what are you using for input, and what are you getting for output?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread