WEEK:4 PROGRAMMING ASSIGNMENT 2 SOLUTION (Jan 2020-Apr2020) An Introduction to Programming through C++ NPTEL 2020

  

               An Introduction To Programming Through C++


IIT Bombay.



Week 4- Programming Assignment 2:


Write a program which simulates the movement of a turtle as follows:
At the beginning, the turtle is at the origin, i.e. point (0,0).  It is facing in the positive x direction. For the purpose of this exercise, let us assume that the coordinate axes are drawn as in mathematics, i.e. the x axis points in the East direction, and the y axis in the North direction.
The user can type commands to move the turtle.  The commands are:
     F : This causes the turtle to move 1 unit in the direction it
     is currently facing.

     L : This causes the turtle to turn left by 90 degrees.

     E : This causes the program to print the current coordinates of
     the turtle and stop execution.

You can assume that the user will not type any other letters besides the ones shown. Note that while there is no command to turn right, we can get the effect by using LLL, i.e. turning left thrice.
As an example, if the user input is "FFLLLFFLFE", then it will cause the turtle to move to (3,-2) and the program will print "3 -2" and stop.

Sample Test Cases
                               Input                  Output
Test Case 1     FFLLLFFLFE        3 -2
Test Case 2     FFFFLLLLFFE       6 0


WRITE/COPY THESE CODE DIRECTLY IN NPTEL TERMINAL AND COMPILE AND RUN THEN SUBMIT.

PROGRAM:
#include<iostream>

int main()
{
  char arr[100];int point[2]={0};
  int i=0,n=0;
  cin>>arr;

  while(arr[i]!='E')
  {
    if(arr[i]=='L')
    {
     n++;
     if(n==4)
     n=0;
    }

    if(arr[i]=='F')
      {

   if(n==1)
     {
  point[1]=point[1]+1;
     }
   else if(n==2)
     {
  point[0]=point[0]-1;

     }
   else if(n==3)
     {
  point[1]=point[1]-1;
     }

   else if(n==0)
     {
  point[0]=point[0]+1;
     }
    }

    i++;
  };


  cout<<point[0]<<" "<<point[1];

  return 0;
}

WEEK-5 :PROGRAMMING ASSIGNMENT 1  AND QUIZ 
CLICK ON BELOW LINK.
<<CLICK HERE FOR  WEEK 5:PROGRAMMING -01>>

<<CLICK HERE FOR  WEEK 5: QUIZ>> FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100%  correct solutions.

No comments:

Post a Comment

If you have any problems related to solutions or any concept please let me know.

Copyright (c) 2020 Custom Programs All Right Reserved

Pages