\ Zero-one Matrix Relations » CS238 Exploration 3
| Tweet |
|
The purpose of this exploration is for you to explore and enhance an implementation of a representation of relations to discover their basic properties.
Requirements
Write a C++ program that takes inputs which are files containing connection (zero-one) matrices of binary
relations of a set with itself. Determine which properties each relation has. Start with the stub code supplied. Add your code and submit the file with the same name (relations.cpp). If conditions are right, you can build and test your code in the Linux Lab via the command: make it just so
What the code does so far
So far my code does the following:
- Determines if reflexive, irreflexive, and nonreflexive
The way I’m determining reflexiveness is by storing the sum of the main diagonal (mSumOfDiagonal) in a member variable. Then the check for reflexiveness is simple:
bool isReflexive()
{
// TRUE if diagonal is all 1
return mSumOfDiagonal == mSize;
}
Check for irreflexiveness is equally obvious:
bool isIrreflexive()
{
// TRUE if diagonal is all 0
return mSumOfDiagonal == 0;
}
Finally, test for nonreflexive (main diagonal has 1s and 0s in it) is equally simple:
bool isNonreflexive()
{
// TRUE if diagonal is all 1
return !isReflexive() && !isIrreflexive;
}
The rest of the code
class Relation operator*(Relation& r1, Relation& r2);
class Relation
{
private:
bool** mMatrix;
int mSize;
int mSumOfDiagonal;
void init()
{
mMatrix = new bool*[mSize];
for (int i = 0; i < mSize; i++)
{
mMatrix[i] = new bool[mSize];
}
}
public:
Relation(int size)
{
mSize = size;
mSumOfDiagonal = 0;
init();
}
Relation& operator=(const Relation& rtSide)
{
if (this == &rtSide)
{
return *this;
}
else
{
mSize = rtSide.mSize;
for (int i = 0; i < mSize; i++)
{
delete [] mMatrix[i];
}
delete [] mMatrix;
init();
for (int x = 0; x < mSize; x++)
{
for (int y = 0; y < mSize; y++)
{
mMatrix[x][y] = rtSide[x][y];
}
}
}
return *this;
}
Relation(const Relation& relation)
{
mSize = relation.getConnectionMatrixSize();
init();
*this = relation;
}
~Relation()
{
for (int i = 0; i < mSize; i++)
{
delete [] mMatrix[i];
}
delete [] mMatrix;
}
int getConnectionMatrixSize() const
{
return mSize;
}
bool* operator[](int row) const
{
return mMatrix[row];
}
bool operator==(const Relation& relation)
{
int size = relation.getConnectionMatrixSize();
if (mSize != size)
{
return false;
}
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (mMatrix[i][j] != relation[i][j])
{
return false;
}
}
}
return true;
}
bool isReflexive();
bool isIrreflexive();
bool isNonreflexive();
bool isSymmetric();
bool isAntisymmetric();
bool isAsymmetric();
bool isTransitive();
void describe();
// Rodrigo's touch
int sumOfDiagonal();
};
ostream& operator<<(ostream& os, const Relation& relation)
{
int n = relation.getConnectionMatrixSize();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
os << relation[i][j] << " ";
}
os << endl;
}
return os;
}
istream& operator>>(istream& is, Relation& relation)
{
int n = relation.getConnectionMatrixSize();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
is >> relation[i][j];
}
}
return is;
}
void Relation::describe()
{
mSumOfDiagonal = sumOfDiagonal();
cout << "\nThe relation represented by the "
<< mSize << "x" << mSize << " matrix\n";
cout << *this << "is\n";
cout << (isReflexive() ? "" : "NOT ") << "Reflexive;\n";
cout << (isIrreflexive() ? "" : "NOT ") << "Irreflexive;\n";
cout << (isNonreflexive() ? "" : "NOT ") << "Nonreflexive;\n";
cout << (isSymmetric() ? "" : "NOT ") << "Symmetric;\n";
cout << (isAntisymmetric() ? "" : "NOT ") << "Antisymmetric;\n";
cout << (isAsymmetric() ? "" : "NOT ") << "Asymmetric; and\n";
cout << (isTransitive() ? "" : "NOT ") << "Transitive.\n";
}
/******************************************************
*
* MAIN
*
******************************************************/
int main(int argc, char* argv[])
{
// __DEBUG__
argc = 2;
argv[1] = "../relations/16.15";
argv[1] = "../relations/16.08";
argv[1] = "../relations/8.3.32.b";
// __DEBUG__
for (int i = 1; i < argc; i++)
{
string file = argv[i];
ifstream inFile(file.c_str());
if (inFile.is_open())
{
int size;
inFile >> size;
Relation relation(size);
inFile >> relation;
inFile.close();
relation.describe();
}
else
{
cout << "Unable to open " + file;
}
}
return 0;
}
int Relation::sumOfDiagonal()
{
int sumOfDiagonal = 0;
// Go through every column
for(int y = 0; y < mSize; y++)
// Go through every row
for(int x = 0; x < mSize; x++)
// Sum the elements in the main diagonal
if(x == y)
sumOfDiagonal += mMatrix[x][y];
cout << sumOfDiagonal << endl;
return sumOfDiagonal;
}
/******************************************************
*
* IS REFLEXIVE
*
* TRUE if main diagonal is all 1. In other words, TRUE
* when sum of main diagonal is the same as the
* size of the matrix (4x4 has size of 4)
*
******************************************************/
bool Relation::isReflexive()
{
return mSumOfDiagonal == mSize;
}
/******************************************************
*
* IS IRREFLEXIVE
*
* TRUE if main diagonal is all 0. In other words, TRUE
* when sum of main diagonal is zero
*
******************************************************/
bool Relation::isIrreflexive()
{
return mSumOfDiagonal == 0;
}
/******************************************************
*
* IS NON REFLEXIVE
*
* TRUE if matrix is neither reflexive or irreflexive
*
******************************************************/
bool Relation::isNonreflexive()
{
return !isIrreflexive() && !isReflexive();
}
/******************************************************
*
* IS SYMMETRIC
*
******************************************************/
bool Relation::isSymmetric()
{
return false;
}
/******************************************************
*
* IS ANTISYMMETRIC
*
******************************************************/
bool Relation::isAntisymmetric()
{
return false;
}
/******************************************************
*
* IS ASYMMETRIC
*
******************************************************/
bool Relation::isAsymmetric()
{
return false;
}
/******************************************************
*
* IS TRANSITIVE
*
******************************************************/
bool Relation::isTransitive()
{
return false;
}
\ Social Media
\ Recent Posts
\ Categories
\ Popular Tags
3D
Artificial Intelligence
BYU-I
C++
C++ to Javascript port
Chromium WebGL
Debugging
demos
Discrete Math
Eclipse
Firebug
Free Download
Google Web Toolkit
GWT
HTML5 Demo
Java
Javascript Games
jQuery
Math & Programming
OBJ File
OpenGL
Papers
Parser
Plug-in
plugins
quick tip
Recurrence Relations
RokkoCode
School Projects
Screen Cast
Screencast
Sudoku
Sudoku Algorithm
The Matrix
WebGL
Web Sockets
wegbl
Writing
writting 3D (5)
Algorithms (1)
Articles (5)
BYU-I (6)
C++ (9)
Fun Projects (3)
GWT (2)
HTML5 (3)
Java (1)
Javascript (7)
PHP (1)
Web 2.0 (2)
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.
