== Programming Assignment == For this assignment you will write a program that c
ID: 3857227 • Letter: #
Question
== Programming Assignment ==
For this assignment you will write a program that controls a set of rovers and
sends them commands to navigate on the Martian surface where they take
samples. Each rover performs several missions and each mission follows the
same sequence: deploy, perform one or more moves and scans, then return to
base and report the results. While on a mission each rover needs to remember
the scan results, in the same order as they were taken, as well as remember
the way to get back to the base. The scan results should be stored in a queue
and the return instructions should be stored in a stack.
The objective of this assignment is to learn how to implement and use both a
queue and a stack. Since the objectives include learning how to implement a
stack and a queue, you cannot use a premade stack or queue (e.g. the STL stack
or queue classes).
== Program Design ==
Your program should use good design methodologies so you should have separate
classes for each of the following:
- rover -- This class represents the rover. Each rover has an ID (integer) as
well as an x, y coordinate to represent its current location relative to the
base. In addition, it needs to have the following public methods:
- void deploy(); -- this deploys a rover on a new mission
- void move(int x, int y) -- this moves a rover to the new x,y coordinates
- void corescan() -- this performs a core sample scan and stores the resuls
- void dock() -- this method returns to the base station and reports the results
Since you are not really on Mars and there aren't really rovers, your rover
class is actually just going to simulate the movements and actions by
printing messages on the screen.
The "deploy" method will print the message "Rover () deploying..."
followed by "Rover () ready.". The string "Rover ()" should include
the actual ID of the rover.
For example, the deploy method does this:
void rover::deploy()
{
printID();
cout << " deploying..." << endl;
printID();
cout << " ready." << endl;
}
The "move" method changes the rover's x,y coordinates, stores the new
location on the stack, and prints a message that indicates the that rover
moved. The message is "Rover () moving to location , .".
The "corescan" method prints a message "Rover () scanning." then calls the
scandata class with the rover's current x, y location, and stores the result
in the results queue.
The "dock" method prints "Rover () returning to base." then uses the entries
from the stack to issue a sequence of moves to backtrack back to the
base. It needs to print out each move (just like the move method). Once at
the base the rover needs to print "Rover () at base. Sending results...". Then
it needs to pull each of the results off of the queue and print them. When
all results are printed the rover needs to print "Rover () result transmission
complete.". Finally, the "dock" method needs to print "Rover () docked.".
Refer to the "expected.txt" file to make sure that your printed messages are
correct.
- stack -- This class should implement a stack. It is used to store the
locations as the rover moves on a mission.
- queue -- This class should implement a queue. It is used to store the scan
results.
== Other Files ==
I have provided a class called "scandata". This class contains all of the
simulated scan data that the rover will read. There is a single method
"getScandata(int x,int y)" that takes in an x, y coordinate and returns the
scan data for that location. You will need to compile the scandata.cpp file
into your application. You should NOT edit or alter the scandata.h or
scandata.cpp files.
I have also provided two test programs: teststack.cpp and testqueue.cpp. These
are for your use. You are not required to use them but they will be helpful
for developing and debugging your queue and stack classes.
Finally, for your convenience I have provided a "makefile". If you name all of
your files the same as I have then you can use the following make commands:
- make rovercontrol -- this builds the entire rovercontrol program
- make testrover -- this builds the rovercontrol program and tests it
against expected.txt
- make testqueue -- this builds the testqueue program and runs it
- make teststack -- this builds the teststack program and runs it
Using the makefile is optional. You are welcome to modify it anyway you
want. You do not need to turn in the makefile.
== External Requirements ==
- The main driver (rovercontrol.cpp) will provide a sequence of commands to a
set of rovers. Each rover needs to respond to the commands. Any number of
rovers can be active at the same time and the commands for different rovers
are interlaced.
- Rover scan results should be reported, after docking, in the same order that
they were collected. You must use a queue to store these.
- Each rover must keep track of the path it follows during its mission so it
can find its way home when the DOCK command is issued. You must use a stack
to store this.
- The output from your program must match expected.txt exactly.
== Internal Requirements ==
- The program must use the supplied rovercontrol.cpp file, unmodified, as the
main driver.
- Each rover must have its own queue and stack.
- All of the scan results must be stored in a queue in each rover.
- All of the route information must be stored in a stack in each rover.
- No memory leaks.
some examples of commands.txt
DEPLOY
1
DEPLOY
2
DEPLOY
3
DEPLOY
4
DEPLOY
5
DEPLOY
6
DEPLOY
7
MOVE
4
4989
5060
MOVE
6
5900
5193
MOVE
5
5838
5002
MOVE
3
5014
5986
MOVE
3
5223
6371
MOVE
4
5637
5769
MOVE
5
6819
5848
MOVE
rovercontrol.cpp
#include
#include
#include
#include "rover.h"
using namespace std;
int main(int argc, char** argv) {
// Set up the program constants
const int NUMBER_OF_ROVERS = 7;
const int MAX_COMMAND_LENGTH = 16;
const int MAX_RESULTS = 128;
const char* CMD_END = "END";
const char* CMD_DEPLOY = "DEPLOY";
const char* CMD_MOVE = "MOVE";
const char* CMD_CORESCAN = "CORESCAN";
const char* CMD_DOCK = "DOCK";
rover** rovers;
if (argc != 2) {
cout << "Usage: " << argv[0] << " " << endl;
return(0);
}
// Create the rovers
rovers = new rover*[NUMBER_OF_ROVERS];
for (int i=0;i {
rovers[i] = new rover(i+1,MAX_RESULTS);
}
// Read the data
char* datafile = argv[1];
ifstream infile(datafile);
char command[MAX_COMMAND_LENGTH];
int roverID;
int commandArg1;
int commandArg2;
infile >> command;
while (strcmp(command,CMD_END) != 0) {
infile >> roverID;
if (strcmp(command,CMD_DEPLOY) == 0)
{
rovers[roverID-1]->deploy();
}
if (strcmp(command,CMD_MOVE) == 0)
{
infile >> commandArg1;
infile >> commandArg2;
rovers[roverID-1]->move(commandArg1, commandArg2);
}
if (strcmp(command,CMD_CORESCAN) == 0)
{
rovers[roverID-1]->corescan();
}
if (strcmp(command,CMD_DOCK) == 0)
{
rovers[roverID-1]->dock();
}
infile >> command;
}
// Free up memory for the allocated rovers
for (int i=0;i {
if (rovers[i] != nullptr)
{
delete rovers[i];
rovers[i] = nullptr;
}
}
delete [] rovers;
}
scandata.h
#ifndef SCANDATA_H
#define SCANDATA_H
#define NUM_SCANDATA_VALUES 1000
class scandata
{
private:
static const int m_data[];
public:
static int getScandata(int x,int y);
};
#endif
scandata.cpp
#include
#include "scandata.h"
int const scandata::m_data[] = {
80, -56, 83, -6, 46, 78, 50, 46, -3, -5, 11, 85, -7, 45, -19, -49, 83,
-50, 21, 24, 1, 47, 20, 68, 21, -56, -31, 83, 95, -10, 43, -15, -42,
-75, -47, 18, 12, -95, 97, 39, 63, -62, -30, 84, 64, -22, 82, 21, -22,
92, -91, 40, 57, 38, 23, -29, -29, 100, 7, -68, 14, 100, -91, 99, -57,
55, 15, -21, -95, -1, 12, -57, -63, -78, 99, -67, -9, -83, -11, 67,
60, 70, -56, -91, 25, -81, -88, -86, 65, -32, -85, 88, -86, 12, 100,
-35, -1, 45, -55, -7, -95, 75, 61, 5, -4, 91, -84, -65, -71, -18, 28,
-28, 15, 5, 26, -4, -97, -89, 98, -54, 13, 100, -43, -24, -16, 72, 85,
23, 11, 94, 35, 39, -10, -90, 51, 90, -59, -64, -25, -100, -72, 11,
14, 76, 0, -64, -79, 38, 41, -6, -54, 41, 99, 60, -63, 31, 31, -92,
-43, 93, -64, -33, 45, -100, 80, 92, -7, -72, -78, -32, -93, 62, 18,
-52, 60, 98, 21, -16, 40, 74, -59, -93, 65, -26, -2, 70, -71, -95, 88,
33, 72, 55, 32, 12, 55, -37, -71, -92, 59, -31, -45, -12, 96, 30, -19,
65, -85, -81, 94, -27, 78, -58, -93, -41, -2, 29, 83, 57, 47, -65,
-38, -80, -6, 44, 65, -65, -94, -37, -28, -3, -61, -70, 77, 49, -82,
-98, -71, 47, -43, -97, 76, 34, -47, -31, -40, 49, -84, -87, -1, -34,
57, -20, -5, 53, -62, -74, 22, -46, 12, -24, -15, 32, 89, 5, 73, 30,
-79, 52, 80, 36, -35, -49, -13, 93, 59, 69, -34, -90, -88, -51, -32,
8, 29, -81, -12, 90, -36, 17, -89, -3, 82, 91, -89, 25, -87, 1, 81,
-32, 19, -63, -85, -78, 34, 42, 23, 9, 5, 14, 71, 56, 32, 62, -91, 84,
-68, -55, -74, 99, 88, -9, -40, -74, 38, 10, -80, 70, -13, 13, 44,
-53, -64, 65, -72, -60, -22, -12, 7, -73, -20, 72, -22, 72, -15, 56,
-54, 4, -66, 50, 89, -71, 89, -50, -85, 69, -93, 99, 71, 84, -71, 94,
-92, 8, -96, -75, -36, 65, 24, -45, -70, 67, -34, -75, 25, 0, 19, -2,
-32, 66, 96, 75, -12, 71, -43, -46, 69, -93, 37, -72, -28, 28, -96,
-39, -78, -19, 65, -25, -46, -38, 78, 12, -94, -28, -96, 32, -59, 37,
62, -57, 100, -32, 73, -91, 9, 2, -51, 71, 60, -42, -48, 98, 13, -37,
56, 15, -66, 90, -62, 31, 65, -27, -18, 94, -50, 52, -56, -33, 87,
-47, -21, -6, -75, 18, 91, -46, -66, -51, -70, -20, 41, -89, 48, -8,
-13, 40, -8, -25, -65, -14, -38, -36, -11, 67, -56, 27, -17, -2, -43,
-37, 100, 42, 10, 35, 49, -7, 45, 12, -51, 41, 73, 40, -82, 73, 25,
-23, -46, 98, -4, 31, -82, 0, -9, 73, -9, 67, -91, -29, 86, -49, -86,
100, 19, -19, 73, -46, -85, -42, 33, 27, -61, 71, -8, -26, -97, 73,
91, -47, 49, -61, -2, -39, 74, 89, -73, 79, 75, -64, 8, 56, 51, -86,
-29, -66, -95, -88, -18, -51, -17, -15, 29, 9, 75, -53, 97, -49, -2,
-56, -65, 80, 95, -20, -14, 25, -35, -26, -49, 30, 0, -27, -62, -42,
-66, -15, -94, -83, -6, 20, -20, -16, 46, -43, 50, 17, 74, -92, -50,
85, -28, -31, 57, -68, 64, -77, -79, -83, -85, 2, -20, -36, 59, -19,
12, 33, -92, -99, 37, 85, -68, 19, -14, 61, 69, 13, 81, -69, 9, 33,
100, -13, 27, 71, -3, 98, -20, -67, -41, -39, -70, -82, 21, -18, -8,
90, 51, 25, 14, -7, 99, -19, -99, 20, 8, -64, -43, 33, 31, 87, 57, 91,
-90, 69, 29, 38, 28, -29, -87, 39, 4, 11, 56, -2, -34, -4, -49, -74,
22, 43, -35, 17, -65, 64, -45, 13, 81, -74, -41, -91, 63, -25, 44, 90,
-96, 95, 32, 16, 56, 22, -13, -59, -6, -26, -77, -84, -100, 52, 89,
-93, 8, 16, -47, -54, 99, -13, -25, 40, -1, 95, -22, -19, -80, -43,
-17, 99, -3, 39, -52, 88, -73, -30, 89, 31, -56, -73, 75, 37, -33, 17,
12, 76, -27, 94, -61, -52, 80, 72, -21, 42, 2, 12, 38, -96, -27, 86,
-18, -35, -80, 4, 15, -51, -3, 1, 85, 21, 73, 33, 9, 84, 0, -67, -32,
-84, -74, -43, 80, 81, 99, 93, -18, -29, -70, 89, -39, 36, 95, 99,
-91, -89, 97, -100, 39, 8, 59, -31, 5, 35, -10, -77, -97, 44, 21, 34,
2, -25, -65, 75, -100, 81, 13, 38, -76, 34, 57, 9, -69, 45, 73, -60,
34, 73, -74, -59, -80, 54, -40, -19, 9, 5, 6, 83, 88, -27, -48, 21,
23, -21, -97, -10, -41, 9, 72, -67, 2, -65, -70, 70, 81, 85, -98, 57,
-79, -2, -34, 75, -5, 45, 55, -84, 96, 94, 49, -3, 67, 8, 57, -45,
-12, 88, -75, 91, -65, 57, -69, 36, 62, 72, 34, 78, 54, -66, -60, 69,
-71, 20, -45, 16, 29, 47, 54, 9, 89, -88, 77, -62, -2, -20, 83, -30,
8, 84, -75, 73, -25, 37, -62, -18, -58, 7, -42, 41, 52, -31, -50, -2,
55, -54, -80, -88, -34, 28, 48, 0, -66, -33, 75, -21, 64, -95, 59, 43,
76, 57, 70, 93, -78, 57, 61, -15, -37, 20, 93, 17, -66, 74, -74, -52,
25, 34, -44, 87, -59, -96, 94, -48, 1, 70, -17, -18, 65, 29, -60, 54,
91, 31, -28, -24, 33, -26, 60, 17, -12, 0, 52, -75, 77, 58, -12, -25,
69, -90, 72, -85, 41, -45, -43, 62, -92, 89, 59, -59, 25, -90, -57,
-85, 22, -57, -73, -66, -10, 22, 45, 96, -79, -50, -21, -16, -17, -40,
-12, 86, -61, 100, 21, -3, 33, -95, -50, -5, -88, -11, 56, 30};
int scandata::getScandata(int x,int y)
{
int ret = 0;
int index = (x * y);
index *= (index < 0) ? -1 : 1; // Make it positive
index = index % NUM_SCANDATA_VALUES;
ret = m_data[index];
return ret;
}
testqueue.cpp
#include
#include "queue.h"
using namespace std;
void dumpQueue(queue &q)
{
cout << "Dumping the queue" << endl;
while (!q.isEmpty())
{
int result = q.dequeue();
cout << "Value: " << result << endl;
}
}
int main()
{
queue q(5);
q.enqueue(1);
dumpQueue(q);
q.enqueue(1);
q.enqueue(2);
// q.printInternals();
dumpQueue(q);
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.enqueue(6);
// q.printInternals();
dumpQueue(q);
}
teststack.cpp
#include
#include "stack.h"
using namespace std;
void dumpStack(stack &s)
{
cout << "Dumping the stack" << endl;
while (!s.isEmpty())
{
const stack_entry* result = s.pop();
cout << "Value: " << result->x << ", " << result->y << endl;
}
}
void pushHelper(stack &s,int x, int y)
{
stack_entry entry;
entry.x = x;
entry.y = y;
s.push(entry);
}
int main()
{
stack s(5);
stack_entry entry;
pushHelper(s,1,1);
dumpStack(s);
pushHelper(s,1,1);
pushHelper(s,2,2);
dumpStack(s);
}
makefile
CC=g++
CFLAGS = -std=c++11 -I.
DEPS = rover.h queue.h stack.h
QOBJ = queue.o
SOBJ = stack.o
OBJ = rover.o rovercontrol.o scandata.o $(QOBJ) $(SOBJ)
%.o: %c $(DEPS)
$(CC) $(CFLAGS) -c -o $@
rovercontrol: $(OBJ)
$(CC) $(CFLAGS) -o $@ $^
testrover: rovercontrol
./rovercontrol commands.txt > tmp.txt
diff tmp.txt expected.txt > diffout.txt
testqueue: $(QOBJ) testqueue.cpp
$(CC) $(CFLAGS) -g -o $@ $^
./testqueue
teststack: $(SOBJ) teststack.cpp
$(CC) $(CFLAGS) -g -o $@ $^
./teststack
some examples of expected.txt
Rover (ID 1) deploying...
Rover (ID 1) ready.
Rover (ID 2) deploying...
Rover (ID 2) ready.
Rover (ID 3) deploying...
Rover (ID 3) ready.
Rover (ID 4) deploying...
Rover (ID 4) ready.
Rover (ID 5) deploying...
Rover (ID 5) ready.
Rover (ID 6) deploying...
Rover (ID 6) ready.
Rover (ID 7) deploying...
Rover (ID 7) ready.
Rover (ID 4) moving to location 4989, 5060.
Rover (ID 6) moving to location 5900, 5193.
Rover (ID 5) moving to location 5838, 5002.
Rover (ID 3) moving to location 5014, 5986.
Rover (ID 3) moving to location 5223, 6371.
Rover (ID 4) moving to location 5637, 5769.
Rover (ID 5) moving to location 6819, 5848.
Rover (ID 3) moving to location 5993, 6543.
Rover (ID 2) moving to location 5499, 4379.
CODE SHOULD BE IN C++
Basically just need to implement a rover class, stack class, and queue class that works with rovercontrol.cpp. For scandata.h and scandata.cpp, they will need to be compiled along with the classes and rovercontrol, but they should not be changed themselves at all. The commands.txt is the file that will be used to send the commands to the rover. testqueue.cpp and teststack.cpp are just used to test the queue and stack classes, but are not required.
Explanation / Answer
////////////////////////////////// Stack Class /////////////////////////////////////
#include<iostream>
#include<cstdlib>
#define MAX_SIZE 10
using namespace std;
class Stack{
private:
int item[MAX_SIZE];
int top;
public:
Stack(){
top = -1;
}
void pushHelper(int data){
item[++top] = data;
}
int dumpStack(){
return -1;
}
int size(){
return top+1;
}
int is_empty(){
if(top==-1)
return 1;
else
return 0;
}
int is_full(){
if(top==MAX_SIZE-1)
return 1;
else
return 0;
}
void display(){
for(int i=0; i<this->size(); i++){
cout<<item[i]<<" ";
}
cout<<endl;
}
};
//////////////////////////////////////////// Queue Class ///////////////////////////////////////
#include<iostream>
#include<cstdlib>
#define MAX_SIZE 10
using namespace std;
class Queue{
private:
int item[MAX_SIZE];
int rear;
int front;
public:
Queue();
void enqueue(int);
int dequeue();
int size();
void display();
bool isEmpty();
bool isFull();
};
Queue::Queue(){
rear = -1;
front = 0;
}
void Queue::enqueue(int data){
item[++rear] = data;
}
int Queue::dequeue(){
return item[front++];
}
void Queue::display(){
if(!this->isEmpty()){
for(int i=front; i<=rear; i++)
cout<<item[i]<<endl;
}else{
cout<<"Queue Underflow"<<endl;
}
}
int Queue::size(){
return (rear - front + 1);
}
bool Queue::isEmpty(){
if(front>rear){
return true;
}else{
return false;
}
}
bool Queue::isFull(){
if(this->size()>=MAX_SIZE){
return true;
}else{
return false;
}
};
//////////////////////////////// Rover Class ////////////////////////////////////
#include <iostream>
using namespace std;
class Rover{
void deploy()
{
cout << " deploying..." << endl;
cout << " ready." << endl;
}
void move(int x, int y)
{
cout<<"Rover () moving to location" << endl;
}
void corescan(){
cout<<"Rover is scaning.."<<endl;
}
void dock()
{
cout<<"Rover returning to the dock."<<endl;
}
};
Just fill up the rover function with stack and queue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.