wad wrong with this code?

This is a discussion on wad wrong with this code? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <math.h> #define PI 3.141593 //// macro (used in earlier tutorial), suitable for one liner function #define ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    wad wrong with this code?

    Code:
    #include <stdio.h>
    #include <math.h>
    #define PI 3.141593
    
    //// macro (used in earlier tutorial), suitable for one liner function
    #define deg_to_rad(deg) (deg * (PI/180.0))
    
    //// function prototypes
    double angle(double x1, double y1, double z1, double x2, double y2, double z2);
    
    //// prototype of function to find the great circle distance between two points
    double gc_dist(double, double, double, double);
    
    int main(void) {
      double lat1, long1, lat2, long2;
      
      printf("Enter latitude north and longitude west ");
      printf("for location 1 : ");
      scanf("%lf %lf", &lat1, &long1);
    
      printf("Enter latitude north and longitude west ");
      printf("for location 2 : ");
      scanf("%lf %lf", &lat2, &long2);
      printf("Great Circle Distance: %.0f km\n", gc_dist(lat1, long1, lat2, long2));
    }
    
    double angle(double x1, double y1, double z1, double x2, double y2, double z2) {
      double dot, dist1, dist2;
     
      dot = x1 * x2 + y1 * y2 + z1 * z2; // find the dot product of the two vectors
      dist1 = sqrt(x1 * x1 + y1 * y1 + z1 * z1); // find length of each vector
      dist2 = sqrt(x2 * x2 + y2 * y2 + z2 * z2);
    
      return acos(dot / (dist1 * dist2));
    }
    	double gc_dist(double lat1, double long1, double lat2, double long2) {
      double rho = 6371, phi, theta, gamma, dot, dist1, dist2, x1,y1,z1,x2,y2,z2;
    
      phi = deg_to_rad(90 - lat1); // convert to spherical coordinates
      theta = deg_to_rad(360 - long1);
      x1 = rho * sin(phi) * cos(theta); // find rectangular coordinates for p1
      y1 = rho * sin(phi) * sin(theta);
      z1 = rho * cos(phi);
      phi = deg_to_rad(90 - lat2); // repeat for p2
      theta = deg_to_rad(360 - long2);
      x2 = rho * sin(phi) * cos(theta);
      y2 = rho * sin(phi) * sin(theta);
      z2 = rho * cos(phi);
      return angle(x1, y1, z1, x2, y2, z2) * rho;
    }
    given
    Enter latitude north and longitude west for location 1 : 36.12 86.67
    Enter latitude north and longitude west for location 2 : 33.94 118.40
    Great Circle Distance: 2886 km

    but i got 3497km...

    did i specify the wrong macro ?
    or i return the wrong value ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Wrap your math in some (). C doesn't do all * before all +.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    no difference in the result ... is still not 2886

    Code:
    double angle(double x1, double y1, double z1, double x2, double y2, double z2) {
      double dot, dist1, dist2;
     
      dot = (x1 * x2) + (y1 * y2) + (z1 * z2); // find the dot product of the two vectors
      dist1 = sqrt((x1 * x1) + (y1 * y1) + (z1 * z1)); // find length of each vector
      dist2 = sqrt((x2 * x2) + (y2 * y2) + (z2 * z2));
    }

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    solved .. deg_to_rad((x-y));
    eg. phi=deg_to_rad((90 - lat1));

    double brackets are needed for the math arithemtic to work
    so if any students are doing this question as well... this is the answer for you guys =)..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 11:52 AM
  2. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

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