I am having trouble finding out how to code this. I understand what each one is and know how to tell by looking but cannot figure out how to create functions to check whether it is either reflexive, symmetric, anti-symmetric, and/or transitive (it can be more than one). Here is the exact problem. Given a set of binary relations, determine whether the set is reflexive, symmetric, anti symmetric and/or transitive. Here are the sets:

0 1 2 3
0 0
1 1
2 2
3 3

x y z
x y
y z
y y
z z

x y z
x x
y z
x y
z y
x z
y y
z x
y x
z z

1 2 3 4 5 6 7 8
1 4
1 7
2 5
2 8
3 6
4 7
5 8
6 6
1 1
2 2

## All 3 Replies

What compiler are you using?

Here is what I have come up with but my functions are not working properly because I am getting false for the first set being reflexive when it should be coming back true. Also, I can't even get my transitive started.. Any help??

``````#include <iostream>

using namespace std;

void reflexive(int a[], int sizeOfA, int b[], int sizeOfB)
{
int i, j;
bool test;
bool hold = true;

for(i = 0; i < sizeOfA; i++)
{
if(hold == true)
{
for(j = 0; j < sizeOfB;)
{
if(b[j] == a[i])
{
hold = true;
break;
}
else
{
hold = false;
cout << "Reflexive - No" << endl;
break;
}
}
}
}
if(hold == true)
{
test = true;
cout << "Reflextive - Yes" << endl;
}
}

void charReflexive(char a[], int sizeOfA, char b[], int sizeOfB)
{
int i, j;
bool test;
bool hold = true;

for(i = 0; i < sizeOfA; i++)
{
if(hold == true)
{
for(j = 0; j < sizeOfB;)
{
if(b[j]==a[i])
{
hold = true;
break;
}
else
{
hold = false;
cout << "Reflexive - No" << endl;
break;
}
}
}
}
if(hold == true)
{
test = true;
cout << "Reflextive - Yes" << endl;
}
}

void symmetric(int a[], int sizeOfA, int b[], int sizeOfB)
{
int i, j;
bool test;
bool hold = true;

for(i = 0; i < sizeOfA; i++)
{
if(hold == true)
{
for(j = 0; j < sizeOfB;)
{
if(a[i] == a[j] && a[i+1] == a[j-1])
{
hold = true;
break;
}
else
{
hold = false;
cout << "Symmetric - No" << endl;
break;
}
}
}
}
if(hold == true)
{
test = true;
cout << "Symmetric - Yes" << endl;
}
}

void charSymmetric(char a[], int sizeOfA, char b[], int sizeOfB)
{
int i, j;
bool test;
bool hold = true;

for(i = 0; i < sizeOfA; i++)
{
if(hold == true)
{
for(j = 0; j < sizeOfB;)
{
if(a[i] == a[j] && a[i+1] == a[j-1])
{
hold = true;
break;
}
else
{
hold = false;
cout << "Symmetric - No" << endl;
break;
}
}
}
}
if(hold == true)
{
test = true;
cout << "Symmetric - Yes" << endl;
}
}

void antiSymmetric(int a[], int sizeOfA, int b[], int sizeOfB)
{
int i, j;
bool test;
bool hold = true;

for(i = 0; i < sizeOfA; i++)
{
if(hold == true)
{
for(j = 0; j < sizeOfB;)
{
if(a[i] <= b[j] && b[j] <= a[i])
{
hold = true;
break;
}
else
{
hold = false;
cout << "Antisymmetric - No" << endl;
break;
}
}
}
}
if(hold == true)
{
test = true;
cout << "Antisymmetric - Yes" << endl;
}
}

void charAntiSymmetric(char a[], int sizeOfA, char b[], int sizeOfB)
{
int i, j;
bool test;
bool hold = true;

for(i = 0; i < sizeOfA; i++)
{
if(hold == true)
{
for(j = 0; j < sizeOfB;)
{
if(a[i] <= b[j] && b[j] <= a[i])
{
hold = true;
break;
}
else
{
hold = false;
cout << "Antisymmetric - No" << endl;
break;
}
}
}
}
if(hold == true)
{
test = true;
cout << "AntiSymmetric - Yes" << endl;
}
}

void transitive(int a[], int sizeOfA, int b[], int sizeOfB)
{

}

void charTransitive(char a[], int sizeOfA, char b[], int sizeOfB)
{

}

int main()
{
char keepGoing = 'y';
while (keepGoing=='y') {

int set1 = {0, 1, 2, 3};
int rel1 = {0, 0, 1, 1, 2, 2, 3, 3};
cout << "Set 1: " << endl;
reflexive(set1, 3, rel1, 4);
symmetric(set1, 3, rel1, 4);
antiSymmetric(set1, 3, rel1, 4);

cout << endl;
char x, y, z;
char set2 = {'x', 'y', 'z'};
char rel2 = {'x', 'y', 'y', 'z', 'y', 'y', 'z', 'z'};
cout << "Set 2: " << endl;
charReflexive(set2, 4, rel2, 8);
charSymmetric(set2, 4, rel2, 8);
charAntiSymmetric(set2, 4, rel2, 8);

cout << endl;
char set3 = {'x', 'y', 'z'};
char rel3 = {'x', 'x', 'y', 'z', 'x', 'y', 'z', 'y', 'x',
'z', 'y', 'y', 'z', 'x', 'y', 'x', 'z', 'z'};
cout << "Set 3: " << endl;
charReflexive(set3, 3, rel3, 18);
charSymmetric(set3, 3, rel3, 18);
charAntiSymmetric(set3, 3, rel3, 18);

cout << endl;
int set4 = {1, 2, 3, 4, 5, 6, 7, 8};
int rel4 = {1, 7, 2, 5, 2, 8, 3, 6, 4, 7, 5, 8, 6, 6, 1, 1,
2, 2};
cout << "Set 4: " << endl;
reflexive(set4, 8, rel4, 20);
symmetric(set4, 8, rel4, 20);
antiSymmetric(set4, 8, rel4, 20);

cout << endl << "Would you like to test it again? (y/n): ";
cin >> keepGoing;
}

return 0;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.