Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- assignment #4
- Dan Davis
- Jan 30, 2012
- csci 1111
- calculates total distance traveled and distance from beginning point to end point
- based on input int pairs
- */
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- int cdist(int x ,int y,int oldx ,int oldy ,int odo);
- int main()
- {
- int x,y,oldx,oldy,odo=0; // (x,y) coordinates, [odo]meter - distance
- float dist;
- while(scanf("%d %d",&x,&y) != EOF)
- {
- if(odo==0) //first values entered are stored for final dist calculation
- {
- oldx = x;
- oldy = y;
- }
- cdist(x ,y ,oldx ,oldy ,odo);
- odo++;
- }
- dist = sqrt((oldx-x)*(oldx-x)+(oldy-y)*(oldy-y));
- printf("%d %d <- Finish\n",x,y);
- printf("Total distance between successive points: %d \n",odo);
- printf("Distance between stant and finish: %f \n",dist);
- return 0;
- }
- int cdist(int x ,int y,int oldx ,int oldy ,int odo)
- {
- char s[6];
- if(x > oldx) strcpy(s,"Right");
- else if(x < oldx) strcpy(s,"Left");
- else if(y > oldy) strcpy(s,"Up");
- else strcpy(s,"Down");
- if(odo == 0)
- printf("%d %d <- Start\n",x,y);
- else
- printf("%d %d <- Distance: %d Direction: %s \n",x,y,odo,s);
- }
Add Comment
Please, Sign In to add comment