Robot co-ordinates ID:6


The initial x and y co-ordinate values of a Robot are passed as the input.
The rest of the input values are the directions in which the Robot moves along with the distance in that direction.
The directions are denoted by N, E, S, W for North, East, South and West.
The program should print the final x and y co-ordinates of the Robot.

The input will be a single string value with the above details separated by one or more spaces.

Boundary Conditions:
The length of the input string will be less than 100.

Example Input/Output:
If the input string is x2 y1 N3 E2 S1 the output must be x4 y3
If the input string is x-2 y3 N1 W3 the output must be x-5 y4

PROGRAM IN C:

 #include <stdio.h>


int main()

{

int sumx=0,sumy=0;

int num;

char ch,ch1;

while(scanf("%c%d",&ch,&num)>0)

{

    if(ch=='x'||ch=='E')

    {

        sumx=sumx+num;

    }

    else if(ch=='W')

    {

        sumx=sumx-num;

    }

    else if(ch=='N'||ch=='y')

    {

        sumy=sumy+num;

    }

    else if(ch=='S')

    {

        sumy=sumy-num;

    }

}

printf("x%d y%d",sumx,sumy);



}

Comments

Popular Posts