# Program That Will Navigate a Robot Across a Marsh in C Assignment Solutions

June 25, 2024
John Smith
🇦🇺 Australia
C
With a master's degree in computer science and over 800 completed orders, John specializes in algorithm design and optimization for Mapping module assignments in C programming. His in-depth knowledge of data structures and efficient coding practices ensures top-notch solutions for every assignment.
Key Topics
• Instructions
• Objective
• Requirements and Specifications
Tip of the day
News

## Instructions

### Objective

Write a program that will navigate a robot across a marsh in C language.

## Requirements and Specifications

Assignment Overview

You are to write a C assignment program that will navigate a robot across a marsh. The marsh contains bogs, obstacles and dry ground. The robot does not have a vision system and moves according to some weird logic. With each step the robot has a 50% chance of stepping forward one step. It has a 10% chance of taking a step backwards, a 20% chance of a step to the right and a 20% chance of a step to the left. The robot can pass safely on dry ground. If the robot attempts to pass through an obstacle or step off the edge of the marsh, it will remain stationary. If it attempts to cross a boggy patch, it will become instantly bogged and the mission aborted. If the robot gets bogged, it will immediately emit a distress signal which includes its current position.

Your program will read a map for a marsh from a file. The map is divided into a grid of squares. Each square on the map corresponds to one step for the robot. The robot can only move to adjacent squares on the same row or column as its current position on the map. The robot does not have access to this file. The robot will start at a position specified by you on the top row of the marsh.

Your program should then simulate the movement of the robot through the marsh until one of three conditions is achieved:

• The robot becomes bogged.
• The user does not wish to continue.
• The robot successfully crosses the marsh.

Successful crossing of the marsh occurs when the robot reaches any area of dry ground on the bottom row of the marsh.

You are guaranteed that the initial square for the robot is dry ground and there is definitely a path through the marsh. Your program must ensure that the robot does not step off the marsh. If the robot attempts to take a step that would result in it moving off the marsh, you must cause it to remain stationary on this step.

Source Code

```#include #include #include #include #define MARSH_SIZE 10 #define LINE_SIZE 256 void display_details() { printf("%-9s: %s\n", "File", "yourEmailId_robot.c"); printf("%-9s: %s\n", "Author", "Your Name"); printf("%-9s: %s\n", "Stud ID", "Your Stud ID"); printf("%-9s: %s\n", "Email ID", "Your Email ID"); printf("%s\n", "This is my own work as defined by the University's Academic Misconduct Policy."); } char get_step_choice() { int goodInput = 0; char line[256]; while(goodInput == 0) { char c; printf("Take another step [y|n]? "); scanf("%s", line); if (strlen(line) > 1 || (line[0] != 'y' && line[0] != 'n')) { printf("Please enter either y or n.\n\n"); } else { return line[0]; } } } void read_marsh(char marsh[][MARSH_SIZE] ) { char c; FILE *f = fopen("marsh.txt", "r"); int i, j; if (!f) { printf("Can not open marsh file"); return; } for(i = 0; i < MARSH_SIZE; i++) { for (j = 0; j < MARSH_SIZE; j++) { fscanf(f, "%c", &c); marsh[i][j] = c; } fscanf(f, "%c", &c); fscanf(f, "%c", &c); } fclose(f); } void display_marsh(char marsh[][MARSH_SIZE], int x, int y, int steps) { int i, j; printf("-- %d steps taken -- position [%d, %d] --\n\n", steps, x, y); printf(" "); ```

``` for(i = 0; i printf("%c", '-'); } printf(" \n"); ```

``` for (i = 0; i printf("|"); ```

``` for (j = 0; j if (i == x && j == y) { printf("*"); } else { if (marsh[i][j] == ' ') { printf("."); } else { printf("%c", marsh[i][j]); } } } printf("|\n"); } printf(" "); ```

``` for(i = 0; i printf("%c", '-'); } printf(" \n"); } char get_direction() { srand(time(0)); int r = rand() % 9; if (r < 5) { return 'f'; } else if (r < 6) { return 'b'; } else if (r < 8) { return 'l'; } else { return 'r'; } } int move_robot(char marsh [][MARSH_SIZE], char dir, int* x, int* y) { if (dir == 'f') { if (*x+1 >= MARSH_SIZE) { return 1; } if (marsh[*x+1][*y] == 'o') { return 0; } *x = *x + 1; return marsh[*x][*y] == 'b' ? -1 : 1; } else if (dir == 'b') { if (*x-1 < 0) { return 1; } if (marsh[*x-1][*y] == 'o') { return 0; } *x = *x - 1; return marsh[*x][*y] == 'b' ? -1 : 1; } else if (dir == 'l') { if (*y-1 < 0) { return 1; } if (marsh[*x][*y-1] == 'o') { return 0; } *y = *y - 1; return marsh[*x][*y] == 'b' ? -1 : 1; } else { if (*y+1 >= MARSH_SIZE) { return 1; } if (marsh[*x][*y+1] == 'o') { return 0; } *y = *y + 1; return marsh[*x][*y] == 'b' ? -1 : 1; } } int main(void) { char marsh[MARSH_SIZE][MARSH_SIZE]; int x = 0; int y = 0; int bogged = 0; int steps = 0; char choice = 'y'; char dir = ' '; int res; display_details(); printf("\n\n"); printf("Please, enter robot starting position [0-9]: "); scanf("%d", &y); printf("\n"); read_marsh(marsh); display_marsh(marsh, x, y, steps); printf("\n"); choice = get_step_choice(); while (choice == 'y' && (x < MARSH_SIZE - 1 && bogged == 0)) { steps++; dir = get_direction(); res = move_robot(marsh, dir, &x, &y); display_marsh(marsh, x, y, steps); if(dir == 'f') { printf("-- Moving forward\n\n"); } else if (dir == 'b') { printf("-- Moving backward\n\n"); } else if (dir == 'l') { printf("-- Moving left\n\n"); } else { printf("-- Moving right\n\n"); } if (res == -1) { printf("-- Help! Stuck in a bog [%d, %d]\n", x, y); bogged = 1; } if (res == 0) { printf("-- Doh! Hit an obstacle\n"); } choice = get_step_choice(); } if (choice == 'n' || bogged == 1) { printf("-- Mission failed at %d steps - \n", steps); } else { printf("-- Success in %d steps --\n", steps); } return 0; } ```

## Similar Samples

Discover our portfolio of exemplary programming homework samples at ProgrammingHomeworkHelp.com. From fundamental algorithms to advanced data structures, our samples exhibit expertise in Python, Java, C++, and more. Each solution demonstrates our dedication to clarity, efficiency, and meeting academic standards. Explore them to gain insights into our approach and trust us with your programming assignments.