Find the final co | C ++ implementation of the above approach ; Function to print the final position of the point after traversing through the given directions ; Traversing through the given directions ; If its north or south the point will move left or right ; If its west or east the point will move upwards or downwards ; Returning the final position ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void finalCoordinates ( int SX , int SY , string D ) { for ( int i = 0 ; i < D . length ( ) ; i ++ ) { if ( D [ i ] == ' N ' ) SY += 1 ; else if ( D [ i ] == ' S ' ) SY -= 1 ; else if ( D [ i ] == ' E ' ) SX += 1 ; else SX -= 1 ; } string ans = ' ( ' + to_string ( SX ) + ' , ' + to_string ( SY ) + ' ) ' ; cout << ans << endl ; } int main ( ) { int SX = 2 , SY = 2 ; string D = " NSSE " ; finalCoordinates ( SX , SY , D ) ; }