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

DATA STRUCTURE/C++ Replace with the 256 element transition table with a 40 eleme

ID: 3834593 • Letter: D

Question

DATA STRUCTURE/C++

Replace with the 256 element transition table with a 40 element transition table. The 40 element transition table's elements are not integers. The elements are triples {source state, destination state, transition type}

The transition type is Bad or Good.

Look at the transition table. B is bad, G is good, and I is impossible. The new transition table will contain no impossible transitions.

(HERE IS THE IMPLEMENTATIONS, CLASSES AND MAIN SOURCE)

SOURCE.cpp

include <iostream>
#include <string>
#include "cargo.h"
#include "river.h"
using namespace std;

int main()
{
River river;
river.show();
do
{
  int cargo = Cargo::read();
  river.cross(cargo);
  river.show();
}
while(!river.stop());

system("pause");
return 0;
}

RIVER.h

#include <math.h>
#include "cargo.h"

class River
{
public:
River(void);
void show(void);
void cross(int);
bool stop(void);

private:
static const int stop_state = 15;
bool _far_side[Cargo::CARGO_AND_SHOWMAN];
bool _near_side[Cargo::CARGO_AND_SHOWMAN];
void copy(bool[], bool[]);
int state(bool[]);
bool goodCrossing(bool [], bool []);
};

RIVER.cpp

#include "River.h"

River::River(void)
{
for (int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
  _far_side[pos] = false;
}
void River::show(void)
{
for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
  if(_far_side[pos])
   cout << "- | " << Cargo::cargoToChar(pos) << endl;
  else
   cout << Cargo::cargoToChar(pos) << " | -" << endl;
cout << endl;
}
void River::cross(int cargo)
{
copy(_far_side, _near_side);
_far_side[Cargo::SHOWMAN] = !_near_side[Cargo::SHOWMAN];
_far_side[cargo] = !_near_side[cargo];
if(!goodCrossing(_near_side, _far_side))
  copy(_near_side, _far_side);
}
void River::copy(bool source[], bool sink[])
{
for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
  sink[pos] = source[pos];
}
int River::state(bool river[])
{
int _state = 0;
for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
  _state = _state + river[pos] * (int)pow(2, pos);
return _state;
}
bool River::goodCrossing(bool this_side[], bool that_side[])
{
static const int I = 0;
static const int B = 1;
static const int G = 2;
static const int n_states = 16;
static int crossing_table[][n_states] =
{{I,I,I,I,B,B,G,I,I,I,I,I,B,I,I,I},
{I,I,I,I,I,B,I,G,I,I,I,I,I,G,I,I},
{I,I,I,I,I,I,G,G,I,I,I,I,I,I,G,I},
{I,I,I,I,I,I,I,B,I,I,I,I,I,I,I,B},
{B,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I},
{B,B,I,I,I,I,I,I,I,I,I,I,I,I,I,I},
{G,I,G,I,I,I,I,I,I,I,I,I,I,I,I,I},
{I,G,G,B,I,I,I,I,I,I,I,I,I,I,I,I},
{I,I,I,I,I,I,I,I,I,I,I,I,B,G,G,I},
{I,I,I,I,I,I,I,I,I,I,I,I,I,G,I,G},
{I,I,I,I,I,I,I,I,I,I,I,I,I,I,B,B},
{I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,B},
{B,I,I,I,I,I,I,I,B,I,I,I,I,I,I,I},
{I,G,I,I,I,I,I,I,G,G,I,I,I,I,I,I},
{I,I,G,I,I,I,I,I,G,I,B,I,I,I,I,I},
{I,I,I,B,I,I,I,I,I,G,B,B,I,I,I,I}};

int this_side_state = state(this_side);
int that_side_state = state(that_side);
return crossing_table[this_side_state][that_side_state] == G;
}
bool River::stop(void)
{
return state(_far_side) == stop_state;
}

CARGO.h

#include <iostream>
#include <limits.h>
#include <string>
using namespace std;

class Cargo
{
public:
static int read(void);
static int charToCargo(char);
static char cargoToChar(int);
static bool isCargo(char);

static const int BADCARGO = -1;
static const int CABBAGES = 0;
static const int GOAT = 1;
static const int SHOWMAN = 2;
static const int WOLF = 3;
static const int NOCARGO = 4;
static const int CARGO_AND_SHOWMAN = 5;
};

CARGO.cpp

#include "Cargo.h"

int Cargo::charToCargo(char cargo)
{
switch(cargo)
{
case 'w': case 'W':
  return WOLF;
case 's': case 'S':
  return SHOWMAN;
case 'g': case 'G':
  return GOAT;
case 'c': case 'C':
  return CABBAGES;
default:
  return NOCARGO;
}
}
char Cargo::cargoToChar(int cargo)
{
switch(cargo)
{
case WOLF:
  return 'W';
case SHOWMAN:
  return 'S';
case GOAT:
  return 'G';
case CABBAGES:
  return 'C';
case NOCARGO:
  return 'N';
default:
  return 'B';
}
}
bool Cargo::isCargo(char candidate)
{
switch(candidate)
{
case 'w': case 'W':
case 's': case 'S':
case 'g': case 'G':
case 'C': case 'c':
case 'N': case 'n':
  return true;
default:
  return false;
}
}
int Cargo::read(void)
{
char cargo;
do
{
  cout << "Enter W for Wolf" << endl;
  cout << "Enter N for no cargo" << endl;
  cout << "Enter G for Goat" << endl;
  cout << "Enter C for cabbages" << endl;
  cout << "Enter cargo - ";
  cin.get(cargo);
  cin.ignore(numeric_limits<int>::max(), ' ');
}
while(!isCargo(cargo));
return charToCargo(cargo);
}

Explanation / Answer

include <iostream>
#include <string>
#include "cargo.h"
#include "river.h"
using namespace std;

int main()
{
River river;
river.show();
do
{
  int cargo = Cargo::read();
  river.cross(cargo);
  river.show();
}
while(!river.stop());

system("pause");
return 0;
}

RIVER.h

#include <math.h>
#include "cargo.h"

class River
{
public:
River(void);
void show(void);
void cross(int);
bool stop(void);

private:
static const int stop_state = 15;
bool _far_side[Cargo::CARGO_AND_SHOWMAN];
bool _near_side[Cargo::CARGO_AND_SHOWMAN];
void copy(bool[], bool[]);
int state(bool[]);
bool goodCrossing(bool [], bool []);
};

RIVER.cpp

#include "River.h"

River::River(void)
{
for (int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
  _far_side[pos] = false;
}
void River::show(void)
{
for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
  if(_far_side[pos])
   cout << "- | " << Cargo::cargoToChar(pos) << endl;
  else
   cout << Cargo::cargoToChar(pos) << " | -" << endl;
cout << endl;
}
void River::cross(int cargo)
{
copy(_far_side, _near_side);
_far_side[Cargo::SHOWMAN] = !_near_side[Cargo::SHOWMAN];
_far_side[cargo] = !_near_side[cargo];
if(!goodCrossing(_near_side, _far_side))
  copy(_near_side, _far_side);
}
void River::copy(bool source[], bool sink[])
{
for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
  sink[pos] = source[pos];
}
int River::state(bool river[])
{
int _state = 0;
for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
  _state = _state + river[pos] * (int)pow(2, pos);
return _state;
}
bool River::goodCrossing(bool this_side[], bool that_side[])
{
static const int I = 0;
static const int B = 1;
static const int G = 2;
static const int n_states = 16;
static int crossing_table[][n_states] =
{{I,I,I,I,B,B,G,I,I,I,I,I,B,I,I,I},
{I,I,I,I,I,B,I,G,I,I,I,I,I,G,I,I},
{I,I,I,I,I,I,G,G,I,I,I,I,I,I,G,I},
{I,I,I,I,I,I,I,B,I,I,I,I,I,I,I,B},
{B,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I},
{B,B,I,I,I,I,I,I,I,I,I,I,I,I,I,I},
{G,I,G,I,I,I,I,I,I,I,I,I,I,I,I,I},
{I,G,G,B,I,I,I,I,I,I,I,I,I,I,I,I},
{I,I,I,I,I,I,I,I,I,I,I,I,B,G,G,I},
{I,I,I,I,I,I,I,I,I,I,I,I,I,G,I,G},
{I,I,I,I,I,I,I,I,I,I,I,I,I,I,B,B},
{I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,B},
{B,I,I,I,I,I,I,I,B,I,I,I,I,I,I,I},
{I,G,I,I,I,I,I,I,G,G,I,I,I,I,I,I},
{I,I,G,I,I,I,I,I,G,I,B,I,I,I,I,I},
{I,I,I,B,I,I,I,I,I,G,B,B,I,I,I,I}};

int this_side_state = state(this_side);
int that_side_state = state(that_side);
return crossing_table[this_side_state][that_side_state] == G;
}
bool River::stop(void)
{
return state(_far_side) == stop_state;
}

CARGO.h

#include <iostream>
#include <limits.h>
#include <string>
using namespace std;

class Cargo
{
public:
static int read(void);
static int charToCargo(char);
static char cargoToChar(int);
static bool isCargo(char);

static const int BADCARGO = -1;
static const int CABBAGES = 0;
static const int GOAT = 1;
static const int SHOWMAN = 2;
static const int WOLF = 3;
static const int NOCARGO = 4;
static const int CARGO_AND_SHOWMAN = 5;
};

CARGO.cpp

#include "Cargo.h"

int Cargo::charToCargo(char cargo)
{
switch(cargo)
{
case 'w': case 'W':
  return WOLF;
case 's': case 'S':
  return SHOWMAN;
case 'g': case 'G':
  return GOAT;
case 'c': case 'C':
  return CABBAGES;
default:
  return NOCARGO;
}
}
char Cargo::cargoToChar(int cargo)
{
switch(cargo)
{
case WOLF:
  return 'W';
case SHOWMAN:
  return 'S';
case GOAT:
  return 'G';
case CABBAGES:
  return 'C';
case NOCARGO:
  return 'N';
default:
  return 'B';
}
}
bool Cargo::isCargo(char candidate)
{
switch(candidate)
{
case 'w': case 'W':
case 's': case 'S':
case 'g': case 'G':
case 'C': case 'c':
case 'N': case 'n':
  return true;
default:
  return false;
}
}
int Cargo::read(void)
{
char cargo;
do
{
  cout << "Enter W for Wolf" << endl;
  cout << "Enter N for no cargo" << endl;
  cout << "Enter G for Goat" << endl;
  cout << "Enter C for cabbages" << endl;
  cout << "Enter cargo - ";
  cin.get(cargo);
  cin.ignore(numeric_limits<int>::max(), ' ');
}
while(!isCargo(cargo));
return charToCargo(cargo);
}