calculating distance between two points... (x and y vars)

This is a discussion on calculating distance between two points... (x and y vars) within the C Programming forums, part of the General Programming Boards category; As homework we were assigned to enter the following code to calculate the distance between two points on the x ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    1

    Angry calculating distance between two points... (x and y vars)

    As homework we were assigned to enter the following code to calculate the distance between two points on the x and y plane. The program should ask the user to enter two points then should calculate the distance between two points and print the distance on the screen.
    My program will compile correctly but when attempting to run the actual program it doesnt do anything and some how completely skips over my main function...
    Any help or corrections will be greatly appriciated!!!!
    Thanks
    Joe
    Code:
     
    Code:
    #include <stdio.h>
    
    
    #include <math.h>
    
    
    struct point
    {
    
    
    float x;
    
    
    float y;
    
    
    };
    void enter_a_point ( struct point pc );
    
    
    float distance (struct point p1, struct point p2);
    
    
    int main (void)
    {
    
    
    struct point pt1, pt2 ;
    
    
    enter_a_point( pt1 ); //get x & y values for pt1
    
    
    enter_a_point( pt2 ); //get x & y values for pt2
    
    
    distance ( pt1, pt2);
    
    
    printf( "Distance between the points = %.2f\n", distance(pt1, pt2) );
    
    
    return 0;
    
    
    }
    
    
    void enter_a_point ( struct point pc  )
       {
       printf( " Enter X value for point\n");
    
    
       scanf( "%f",&pc.x );
    
    
       printf( " Enter Y value for point\n");
    
    
       scanf( "%f",&pc.y);
       }
    
    
    float distance (struct point p1, struct point p2)
       {
    float d;
    
    
     d = sqrtf( powf((p2.x-p1.x),2)+ pow((p2.y-p1.y),2) );
    
    
    return d;
       }


  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Sounds like some aggrressive compiler optimisation! I could see similar with GCC at -O2.

    The compiler has determined that some of your code is pointless and can be removed.

    Code:
    void enter_a_point ( struct point pc );
    struct point pt1, pt2 ;
     
     
    enter_a_point( pt1 ); //get x & y values for pt1
    The reason is that you've passed a struct as an argument to enter_a_point. Recall that in C, all arguments are passed by value. This means that another local copy of the struct is created and used in enter_a_point. The structs as seen in main() aren't changed.

    Since the compiler knows this and thinks it's a waste of CPU cyces modifying the copied struct, it can optimise quite brutally. It should still prompt you to enter numbers and do the calculation -- if it didn't, that's a bit worrying. Optimisation shouldn't change the behaviour of the program.

    For this code, the solution is to pass the address of the structs:

    Code:
    void enter_a_point ( struct point *pc );  // * indicates pointer
    
    void enter_a_point ( struct point* pc  )
       {
       printf( " Enter X value for point\n");
     
     
       scanf( "%f",&pc->x );  //  use -> to access members
    
    main()
    enter_a_point( &pt1 ); //get x & y values for pt1    // call with address-of operator

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,206
    Quote Originally Posted by Joejones View Post
    [SIZE=2][FONT=arial]
    My program will compile correctly but when attempting to run the actual program it doesnt do anything and some how completely skips over my main function...
    That code does not "skip over" the main function. If your program is not asking you for the X and Y value for points, then you've made a mistake in building (eg haven't added the source file to a project).

    However, I think you've mis-described your problem ....

    The main problem I can see is that enter_a_point accepts its argument by value. That means, enter_a_point(pt1) in main() passes a COPY of pt1 to the function. The function then changes data in the COPY. Those changes do not affect pt1 within main().

    If you want the changes that enter_a_point() does to be visible to the caller, then change the function so it accepts a pointer.
    Code:
    void enter_a_point ( struct point *pc  )
    {
       printf( " Enter X value for point\n"); 
      scanf( "%f",&pc->x );
      
       printf( " Enter Y value for point\n");
       scanf( "%f",&pc->y);
    }
    Apart from deleting irrelevant white lines, I've highlighted changes needed in red.

    Note you will also need to change the declaration of enter_a_point() that is before main() to match this. Also, the way that main() calls enter_a_point() will need to be changed slightly. I'll leave fixing those things as a small learning exercise for you.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 03-06-2012, 09:24 PM
  2. Problems calculating distance and mpg
    By levitylek in forum C Programming
    Replies: 5
    Last Post: 09-09-2010, 08:41 AM
  3. Replies: 6
    Last Post: 01-08-2006, 01:49 PM
  4. Calculating points of a line
    By Iamien in forum C++ Programming
    Replies: 3
    Last Post: 06-21-2004, 07:40 PM

Tags for this Thread


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