Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1101909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:06:11+00:00 2026-05-17T01:06:11+00:00

I am supposed to write a program in C only. It’s code for a

  • 0

I am supposed to write a program in C only. It’s code for a grid follower.

I have defined a co-ordinate system of the grid (0-5,0-5). Also the bot orientations (+y,-y,+x,-x) and positions are defined. (I would welcome tips on how to write good code for this.)

Now as my bot moves through the grid and goes to some coordinate (x,y) in the grid through some path. Only 90 deg and 180 deg turns are allowed.

How can I reach (0,0), i.e. the starting point, traversing the same path? How can I reverse the orientations and give appropriate turning instructions using C code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:06:12+00:00Added an answer on May 17, 2026 at 1:06 am

    This can be cleaned up quite a lot, but it should be a good framework to understand the concepts. All we’re basically doing is saving every move, and then simply backtracking it.

    #include <stdio.h>
    
    #define MAX_STEPS 256
    
    enum CARDINAL_DIRECTIONS { N = 0, W, S, E };
    
    struct _s_robot {
        int x;
        int y;
        int orientation;
        int step;
        int x_history[MAX_STEPS];
        int y_history[MAX_STEPS];
        int turn_history[MAX_STEPS];
    } MY_LITTLE_ROBOT = {0, 0, 0, 0, {0}, {0}, {0}};
    
    void robot_go() {
        switch(MY_LITTLE_ROBOT.orientation) {
        case N:
            ++MY_LITTLE_ROBOT.y;
            break;
        case W:
            --MY_LITTLE_ROBOT.x;
            break;
        case S:
            --MY_LITTLE_ROBOT.y;
            break;
        case E:
            ++MY_LITTLE_ROBOT.x;
            break;
        }
        MY_LITTLE_ROBOT.x_history[MY_LITTLE_ROBOT.step] = MY_LITTLE_ROBOT.x;
        MY_LITTLE_ROBOT.y_history[MY_LITTLE_ROBOT.step] = MY_LITTLE_ROBOT.y;
        MY_LITTLE_ROBOT.turn_history[MY_LITTLE_ROBOT.step] = MY_LITTLE_ROBOT.orientation;
        ++MY_LITTLE_ROBOT.step;
    }
    
    void robot_change_orientation(int _orientation) {
        MY_LITTLE_ROBOT.orientation = _orientation;
    }
    
    void robot_reverse_orientation(int _orientation) {
        if (_orientation == N) MY_LITTLE_ROBOT.orientation = S;
        else if (_orientation == W) MY_LITTLE_ROBOT.orientation = E;
        else if (_orientation == S) MY_LITTLE_ROBOT.orientation = N;
        else if (_orientation == E) MY_LITTLE_ROBOT.orientation = W;
    }
    
    void robot_backtrack() {
        int i;
        printf("MY_LITTLE_ROBOT wants to turn around, currently at: %i, %i\n", MY_LITTLE_ROBOT.x, MY_LITTLE_ROBOT.y);
        robot_reverse_orientation(MY_LITTLE_ROBOT.orientation);
        for (i = MY_LITTLE_ROBOT.step-1; i >=  0; --i) {
            printf("Robot is @ %i, %i, with an orientation of: %i \n", MY_LITTLE_ROBOT.x, MY_LITTLE_ROBOT.y,  MY_LITTLE_ROBOT.orientation );
            robot_reverse_orientation(MY_LITTLE_ROBOT.turn_history[i]);
            robot_go();
        }
    }
    
    void dump_history() {
        int i;
        for (i = MY_LITTLE_ROBOT.step-1; i >=  0; --i) {
            printf("Robot is @ %i, %i, with an orientation of: %i \n", MY_LITTLE_ROBOT.x_history[i], MY_LITTLE_ROBOT.y_history[i],  MY_LITTLE_ROBOT.turn_history[i] );
        }
    }
    
    int main() {
        printf("***START: Robot is @ %i, %i\n", MY_LITTLE_ROBOT.x, MY_LITTLE_ROBOT.y);
        robot_go();
        robot_go();
        robot_go();
        robot_change_orientation(S);
        robot_go();
        robot_go();
        robot_change_orientation(W);
        robot_go();
        robot_go();
        robot_go();
        robot_change_orientation(N);
        robot_go();
        dump_history();
        robot_backtrack();
        printf("***FINISH: Robot is @ %i, %i\n", MY_LITTLE_ROBOT.x, MY_LITTLE_ROBOT.y);
        return 0;
    }
    

    To use the robot over and over again, you may have to reset all travel history after a backtrack, which should be somewhat trivial. I purposely avoided mallocs and other confusing aspects of making the program actually useful for the sake of verbosity and simplicity. Hopefully it helps.

    I also assumed you didn’t want a true path-finding algorithm (such as A*), because that’s a different ballgame altogether.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write an address book program in C# 2008. It is supposed
I am supposed to write a program in JavaScript to find all the anagrams
I am supposed to write a program that gets some PNG images from the
Given the following code (it's supposed to write helloworld in a helloworld file, and
This code is supposed to read postfix problems from a file and and write
I am supposed to write a Java program to sum the following sequence: 1.0/1.0
I am trying to write simple program using ONLY for loop ( if then
I was given an assignment to write a program to print only even numbers
We are supposed to write a program to solve the following initial value problem
I'm supposed to write a program that does 2 + 2 = 4 and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.