Answered step by step
Verified Expert Solution
Question
1 Approved Answer
HELP! I have a problem with a program I m working on . My program outputs incorrect paths and it doesn t output the expected
HELP! I have a problem with a program Im working on My program outputs incorrect paths and it doesnt output the expected result. For example, when I input aout it should output:
NNENE
NNEEN
NENNE
NENEN
NEENN
ENNEN
ENENN
Number of paths:
But instead it outputs:
Paths:
Number of paths:
Heres my code:
greedyrobot.h
#ifndef GREEDYROBOTH
#define GREEDYROBOTH
#include
#include
Function to generate paths from current position to treasure
void generatePathsint maxdistance, int robotx int roboty int treasurex int treasurey
std::string pathSoFar, std::vector& result;
Function to find paths from starting position to treasure
std::vector findPathsint maxdistance, int robotx int roboty int treasurex int treasurey;
#endif GREEDYROBOTH
greedyrobot.cpp
#include
#include "greedyrobot.h
using namespace std;
void generatePathsint maxdistance, int robotx int roboty int treasurex int treasurey
std::string pathSoFar, std::vector& result
Base case: you've reached the treasure with the required distance
if robotx treasurex && roboty treasurey && pathSoFar.length maxdistance
result.pushbackpathSoFar;
return;
Move East if robot's xcoordinate is less than treasure's xcoordinate
if robotx treasurex && pathSoFar.length maxdistance
generatePathsmaxdistance, robotx roboty treasurex treasurey pathSoFar E result;
Move West if robot's xcoordinate is greater than treasure's xcoordinate
if robotx treasurex && pathSoFar.length maxdistance
generatePathsmaxdistance, robotx roboty treasurex treasurey pathSoFar W result;
Move North if robot's ycoordinate is less than treasure's ycoordinate
if roboty treasurey && pathSoFar.length maxdistance
generatePathsmaxdistance, robotx roboty treasurex treasurey pathSoFar N result;
Move South if robot's ycoordinate is greater than treasure's ycoordinate
if roboty treasurey && pathSoFar.length maxdistance
generatePathsmaxdistance, robotx roboty treasurex treasurey pathSoFar S result;
std::vector findPathsint maxdistance, int robotx int roboty int treasurex int treasurey
std::vector result;
generatePathsmaxdistance, robotx roboty treasurex treasurey result;
return result;
main.cpp
#include
#include "greedyrobot.h
using namespace std;
int mainint argc, char argv
Check if correct number of commandline arguments is provided
if argc
cerr "Usage: argv maxdistance robotx roboty treasurex treasurey
;
return ;
Convert commandline arguments to integers
int maxdistance stoiargv;
int robotx stoiargv;
int roboty stoiargv;
int treasurex stoiargv;
int treasurey stoiargv;
Find paths from starting position to treasure
vector paths findPathsmaxdistance, robotx roboty treasurex treasurey;
Print the paths and number of paths
cout "Paths:
;
for const auto& path : paths
cout path
;
cout "Number of paths: paths.size
;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started