C Program to Find the Destination using Coordinates and Directions

This C Program finds the destination using steps moved and the direction of movement it calculates and finds the final position
[cc lang=”c”]
#include
main()
{
#define MAXWIDTH 10 /* let’s set the maximum width … */
#define MAXHEIGHT 10 /* … and the maximum height */

int a[9][9]; /* Current coordinates */
char direction; /* Direction of movement – N, S, E, W */
int distance; /* Size of movement */
int instructionCount; /* Number of instructions */
int i,j; /* For-loop variable */
int x,y;
int t1,t2;
int b[0][0];

printf(“Enter the start point (x,y): “);
scanf(“%d”,&a[0][0]);

//x%=MAXWIDTH; /* Make sure that the coordinates wrap so that they’re still in the matrix */
//y%=MAXHEIGHT;
/* Read the number of moves */
printf(“Enter the Number of Instructions n”);
scanf(“%d”,&instructionCount);

/* process each instruction */
for(i=0; i MAXHEIGHT)
y = MAXHEIGHT;
break;
/* South is down (towards zero) */
case ‘s’:
case ‘S’: a[i][j]=a[i-1][(i-1)-distance];
if (y < 0) y = 0; break; /* East is to the right (towards the max) */ case 'e': case 'E': a[i][j]=a[(i-1)+distance][i-1]; if (x > MAXWIDTH)
x = MAXWIDTH;
break;

/* West is to the left (towards zero) */
case ‘w’:
case ‘W’:a[i][j]=a[(i-1)-distance][i-1];
if (x < 0) x = 0; break; /* Something incorrect was provided */ default : printf("Error in input detectedn"); a[i][j]=b[0][0]; } } } printf("The Final Destination is %dn", b[0][0]); return 0; /* Needed to prevent a compiler warning */ } } [/cc]

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *