Guest User

Untitled

a guest
Jun 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. /*
  2. assignment #4
  3. Dan Davis
  4. Jan 30, 2012
  5. csci 1111
  6.  
  7. calculates total distance traveled and distance from beginning point to end point
  8. based on input int pairs
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <math.h>
  13. #include <string.h>
  14.  
  15. int cdist(int x ,int y,int oldx ,int oldy ,int odo);
  16.  
  17. int main()
  18. {
  19.     int x,y,oldx,oldy,odo=0;  // (x,y) coordinates, [odo]meter - distance
  20.     float dist;
  21.  
  22.     while(scanf("%d %d",&x,&y) != EOF)
  23.     {
  24.         if(odo==0)  //first values entered are stored for final dist calculation
  25.         {          
  26.             oldx = x;
  27.             oldy = y;
  28.         }  
  29.  
  30.         cdist(x ,y ,oldx ,oldy ,odo);  
  31.         odo++;
  32.     }    
  33.  
  34.     dist = sqrt((oldx-x)*(oldx-x)+(oldy-y)*(oldy-y));
  35.  
  36.     printf("%d %d <- Finish\n",x,y);
  37.     printf("Total distance between successive points: %d \n",odo);
  38.     printf("Distance between stant and finish: %f \n",dist);
  39.      
  40.     return 0;
  41. }
  42.  
  43. int cdist(int x ,int y,int oldx ,int oldy ,int odo)
  44. {
  45.     char s[6];
  46.     if(x > oldx) strcpy(s,"Right");
  47.         else if(x < oldx) strcpy(s,"Left");
  48.         else if(y > oldy) strcpy(s,"Up");
  49.         else strcpy(s,"Down");
  50.  
  51.     if(odo == 0)
  52.         printf("%d %d <- Start\n",x,y);
  53.     else
  54.         printf("%d %d <- Distance: %d Direction: %s \n",x,y,odo,s);
  55. }
Add Comment
Please, Sign In to add comment