Catcoder mars rover solution in c++

Here is an example of a solution for the Catcoder Mars Rover problem in C++, along with explanations for each step:

#include <iostream>
#include <vector>

using namespace std;

// Define a struct to represent the Rover
struct Rover {
    int x, y;
    char direction;
};

// Function to move the Rover forward
void moveForward(Rover& rover) {
    if (rover.direction == 'N') {
        rover.y++;
    } else if (rover.direction == 'S') {
        rover.y--;
    } else if (rover.direction == 'E') {
        rover.x++;
    } else if (rover.direction == 'W') {
        rover.x--;
    }
}

// Function to turn the Rover left
void turnLeft(Rover& rover) {
    if (rover.direction == 'N') {
        rover.direction = 'W';
    } else if (rover.direction == 'S') {
        rover.direction = 'E';
    } else if (rover.direction == 'E') {
        rover.direction = 'N';
    } else if (rover.direction == 'W') {
        rover.direction = 'S';
    }
}

// Function to turn the Rover right
void turnRight(Rover& rover) {
    if (rover.direction == 'N') {
        rover.direction = 'E';
    } else if (rover.direction == 'S') {
        rover.direction = 'W';
    } else if (rover.direction == 'E') {
        rover.direction = 'S';
    } else if (rover.direction == 'W') {
        rover.direction = 'N';
    }
}

// Function to execute the instructions for the Rover
void executeInstructions(Rover& rover, string instructions) {
    for (char instruction : instructions) {
        if (instruction == 'F') {
            moveForward(rover);
        } else if (instruction == 'L') {
            turnLeft(rover);
        } else if (instruction == 'R') {
            turnRight(rover);
        }
    }
}

int main() {
    // Create a Rover object and set its initial position and direction
    Rover rover;
    rover.x = 0;
    rover.y = 0;
    rover.direction = 'N';

    // Get the instructions from the user
    string instructions;
    cout << "Enter the instructions: ";
    cin >> instructions;

    // Execute the instructions for the Rover
    executeInstructions(rover, instructions);

    // Print the final position and direction of the Rover
    cout << "Final position: (" << rover.x << ", " << rover.y << ")" << endl;
    cout << "Final direction: " << rover.direction << endl;

    return 0;
}

Explanation of the solution:

  • We start by including the necessary header files, iostream and vector.
  • We declare a struct called Rover to represent the Mars Rover. It contains three variables: x and y to represent the position of the Rover, and direction to represent the direction it is facing.
  • We define three functions: moveForward, turnLeft, and turnRight, to handle the corresponding actions for the Rover.
  • The moveForward function changes the position of the Rover based on its current direction.
  • The turnLeft function updates the direction of the Rover by rotating it 90 degrees counterclockwise.
  • The turnRight function updates the direction of the Rover by rotating it 90 degrees clockwise.
  • We define the executeInstructions function to execute a series of instructions for the Rover. It takes the Rover object and a string of instructions as parameters. It iterates over each instruction and calls the corresponding function to perform the action.
  • In the main function, we create a Rover object and initialize its position and direction.
  • We prompt the user to enter the instructions for the Rover.
  • We call the executeInstructions function to execute the instructions for the Rover.
  • Finally, we print the final position and direction of the Rover.

This solution allows the user to input a series of instructions (e.g., "FFRFFLFF"), and the program will calculate the final position and direction of the Rover based on those instructions.