Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need the program to be written in C++. also the input.txt file is given to nav

ID: 3586099 • Letter: I

Question

I need the program to be written in C++. also the input.txt file is given to navigate the car from start to finish avoiding obstacles.

In this assignment, you are expected to write a program to navigate a car from start point to finish point avoiding obstacles. You are given that there will always be a path. The car can move up, down, left or right but NOT diagonal. . The map should be built based on the input given in the input file where each dataset is delimited by -1. A data set will comprise of the map size, starting point, end point and obstacle locations. You are to decide on the strategy of the move based on the current car position and the end point location. Since you will be working with multiple data sets with varying size of maps, you would want to use multi-dimensional arrays and dynamic memory allocation.

To summarize the steps

1. Create the navigator object and open the input file.

2. Read data set from the input file

3. Plot(print) the map which consists routes and obstacles with car at the given start point **

4. Navigate to the end position – this involves repetition of the following steps:

i. Deciding on the strategy of the move & making the move

ii. Printing the updated map **

5. When the car reaches the end point, print the number of steps taken to reach the goal **

6. Repeat step 2-5 till end of file

** Output should go to both the screen and an output file.

Programming concepts that are expected in the solution:

1. The program should consist of at least two modules:

i. Navigator module

ii. Test module

2. Following good object oriented programming practices

i Keep the data members private

ii. Accessor function should be declared to set and get the private data if needed.

3. Use operator “<<” overloading to print (plot) the map. Overloading the input operator “>>” to read the data set is encouraged but is optional.

4. Use multi-dimensional arrays and dynamic memory allocation

Input.txt Size Start point End point Obstacles Delimiter (6, 5) 1,1 (3, 5) 1, 3 (2, 4)

Explanation / Answer

#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
//Class CarPosition definition
class CarPosition
{
//Data member declaration
//To store board value
string **matrix;
//To store row and column size of the matrix
int sizeR, sizeC;
//To store starting row and column position
int startPoint[2];
//To store ending row and column position
int endPoint[2];
//To store Obstracles row and column position
int obstracles[20];
//To store number of obstracles
int numberOfObst;
public:
//Member function prototype
void readFile();
void displayBoard();
void createBoard();
void moveCar(int &, int &);
int getStarPoint1();
int getStarPoint2();
int getEndPoint1();
int getEndPoint2();
};//End of class

//Function to return destination row position
int CarPosition::getEndPoint1()
{
return endPoint[0];
}//End of function

//Function to return destination column position
int CarPosition::getEndPoint2()
{
return endPoint[1];
}//End of function

//Function to return starting row position
int CarPosition::getStarPoint1()
{
return startPoint[0];
}//End of function

//Function to return starting column position
int CarPosition::getStarPoint2()
{
return startPoint[1];
}//End of function

//Function to display the board values
void CarPosition::displayBoard()
{
//Loops till row size
for(int r = 0; r < sizeR; r++)
{
//Loops till column size
for(int c = 0; c < sizeC; c++)
{
cout<<matrix[r][c]<<" ";
}//End of inner for loop
cout<<endl;
}//End of outer for loop
}//End of function

//Function to create the board by assigning "*", "C", "O" and "E"
void CarPosition::createBoard()
{
//Loops till row size
for(int r = 0; r < sizeR; r++)
{
//Loops till row size
for(int c = 0; c < sizeC; c++)
{
//Assigns "*"
matrix[r][c] = "*";
}//End of inner for loop
}//End of outer for loop
//Assigns "E" for end position of car at specified end row and column position in the file
matrix[endPoint[0]][endPoint[1]] = "E";
//Assigns "C" for initial car position at specified start row and column position in the file
matrix[startPoint[0]][startPoint[1]] = "C";
//Loops till number of obstracles count
for(int t = 0; t < numberOfObst; t++)
//Assigns "O" for Obstracle position at specified obstracle row and column position in the file
matrix[obstracles[t]][obstracles[++t]] = "O";
}//End of function


//Read data from file
void CarPosition::readFile()
{
//Creates an object of ifstream
ifstream readf;
//Opens the file for reading
readf.open ("InputData.txt");
int obstraclesCounter = 0;
//Reads data and stores in respective data members
//Row size
readf>>sizeR;
//Column size
readf>>sizeC;
//Dynamically allocate memory for number of rows
matrix = new string*[sizeR];
//Dynamically allocate memory for each column
for( int i = 0 ; i < sizeR; i++ )
matrix[i] = new string[sizeC];
//Starting row position for car
readf>>startPoint[0];
//Starting column position for car
readf>>startPoint[1];
//Ending row position for car
readf>>endPoint[0];
//Ending column position for car
readf>>endPoint[1];

//One subtracted because matrix row and column index position starts from zero
//Starting row position for car
startPoint[0]-- ;
startPoint[1]--;
endPoint[0]--;
endPoint[1]--;
//Loops till end of file
do
{
//Temporary variable to store obstracles
int t;
//Read the obstracles from the file and stores it in t
readf>>t;
//Checks if it is -1 end point for file
if(t == -1)
//Come out of the loop
break;
//If not -1
else
//One subtracted because matrix row and column index position starts from zero
//Stores the t value in the array obstracles counter position
obstracles[obstraclesCounter] = --t;
//Increase the obstracle counter
obstraclesCounter++;
}while(1);//End of while
//Stores the obstracles counter value in number of obstracles
numberOfObst = obstraclesCounter;
//Close file
readf.close();
}//End of function

//Function to move the car
void CarPosition::moveCar(int &start1, int &start2)
{
//Initializes the flag to zero
int flag = 0;
//Loops till row size
for(int r = start1; r < sizeR; r++)
{
//Loops till column size
for(int c = start2; c < sizeC; c++)
{
//Checks if current row and column position of the matrix contains "*"
if(matrix[r][c] == "*" )
{
//Assign "C" at the current position
matrix[r][c] = "C";
//Assign "*" in the starting row and column position where "C" was
matrix[start1][start2] = "*";
//Update current start row position to current r value
start1 = r;
//Update current start column position to current c value
start2 = c;
//Set the flag value to 1 for found "*"
flag = 1;
//Come out of the inner loop
break;
}//End of if condition
//Otherwise check if current row and column position of the matrix contains "O"
else if(matrix[r][c] == "O")
{
//Set the flag to zero
flag = 0;
//Come out of the inner loop
break;
}//End of else if
//Otherwise check if current row and column position of the matrix contains "E"
else if(matrix[r][c] == "E")
{
//Assign "C" at the current position
matrix[r][c] = "C";
//Assign "*" in the starting row and column position where "C" was
matrix[start1][start2] = "*";
//Update current start row position to current r value
start1 = r;
//Update current start column position to current c value
start2 = c;
//Set the flag value to 1 for found "E"
flag = 1;
//Come out of the inner loop
break;
}//End of else if
}//End of inner for loop
//Check if the flag value is one then come out of the outer loop
if(flag == 1)
//Come out of the outer loop
break;
}//End of outer for loop
}//End of function

//Main function definition
int main()
{
//Creates CarPosition class object
CarPosition cp;
//Calls the function to read data from file
cp.readFile();
//Calls the function to create the board
cp.createBoard();
//To store starting row and column position
int start1, start2;
//Stores the starting row position
start1 = cp.getStarPoint1();
//Stores the starting column position
start2 = cp.getStarPoint2();
//Initialize the counter by one
int counter = 1;
cout<<" **************** Starting Config ************** ";
//Displays initial board value
cp.displayBoard();
//Loops till current row and column position is not equal to end point row and column position
do
{
//Calls the function to move the car
cp.moveCar(start1, start2);
//Displays step value
cout<<" **************** Step "<<counter<<" ************** ";
//Calls the function to display the updated board
cp.displayBoard();
//Checks if current row and column position is not equal to end point row and column position
if(start1 == cp.getEndPoint1() && start2 == cp.getEndPoint2())
//Come out of the loop
break;
//Increase the counter by one
counter++;
}while(true);//End of do - while loop
//Displays number of steps
cout<<" Total step to the goal: "<<counter;
}//End of function

Sample Run:


**************** Starting Config **************
C * O * *
O * * O *
* * * * E
* * * * *
* * * * *
* * * * *

**************** Step 1 **************
* C O * *
O * * O *
* * * * E
* * * * *
* * * * *
* * * * *

**************** Step 2 **************
* * O * *
O C * O *
* * * * E
* * * * *
* * * * *
* * * * *

**************** Step 3 **************
* * O * *
O * C O *
* * * * E
* * * * *
* * * * *
* * * * *

**************** Step 4 **************
* * O * *
O * * O *
* * C * E
* * * * *
* * * * *
* * * * *

**************** Step 5 **************
* * O * *
O * * O *
* * * C E
* * * * *
* * * * *
* * * * *

**************** Step 6 **************
* * O * *
O * * O *
* * * * C
* * * * *
* * * * *
* * * * *
Total step to the goal: 6

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote