# how to check a realations ? (Math not real life)

This is a discussion on how to check a realations ? (Math not real life) within the C Programming forums, part of the General Programming Boards category; Hello,... im going over over course that was tought by Dr. David Tankus : "Logic and formal Language". one of ...

1. ## [Tutorial] how to check a realations ? (Math not real life)

Hello,...

im going over over course that was tought by Dr. David Tankus : "Logic and formal Language".

one of the things that distinct this lecturar (i don't know how to call him in the right form) is that any mathimatical question should be solved by c programming (wonder why ... ).

any why
one of the ways to check relations is using a binary matrix (the value can be 0 or 1).

The matrix is created when the lines are the domain and coulmuns are the range.

for example the relation :

A = {1,2,3,4}
B = {1,2,4,6}

R = A toward B

R = {(1,1) , (2,2),(3,4),(4,6)}

then the domain is : (1,2,3,4)
and range is (1,2,4,6).

so lets go to the issues :
Relation is Reflexive if aRa (each pair is in relation with it self) by defention it means that any object (i don't how to call this in english).

so all diagnel in the matrix should be ones
so:
i = lines.
J = coulmuns.
Code:
```#define true 1
#define false 0

int check_for_reflexive(int **matrix)
{
for (i =0 ; i < lines;i++)
{
if (matrix[i][i] == 0)
{
return false;
}
}
return true;
}```
Ireflexive:
a not R a: (object not in relation with it self)
the diagnnel should be all 0.
Code:
```[CODE]
int check_for_Ireflexive(int **matrix)
{
for (i =0 ; i < lines;i++)
{
if (matrix[i][i] == 1) //see the deference ?
{
return false;
}
}
return true;
}
[/CODE]```
Semtric:
aRb and bRa dlarrow semtric:
(1,2) in R and (2,1) in R
so:

Code:
```[CODE]
int check_for_semtric(int **matrix)
{
for (i =0 ; i < lines;i++)
{
for (j =0;j <colmuns;j++)
{
if (matrix[i][j] == 1))
if (matirx[j][i] == 0)
return false;
}
}
return true;
}
[/CODE]```
antisemtric:
aRb and bRa dlrarrow a = b.
so ...

Code:
```int check_for_antisemtric(int **matrix)
{
for (i =0 ; i < lines;i++)
for (j =0;j <colmuns;j++)
{
if ((matrix[i][j] == 1)) && (matrix[j][i] ==1))
{
if (i != j)
return false;
}

}
return true;
}```
Asemtirc:
if aRb then b not R a
Code:
```int check_for_antisemtric(int **matrix)
{
for (i =0 ; i < lines;i++)
for (j =0;j <colmuns;j++)
{
if ((matrix[i][j] == 1)) && (matrix[j][i] ==1))
{
return false;
}

}
return true;
}```

Transtive:
if aRb and bRc then aRc example : 1 <3<4 -> 1<4
Code:
```int check_for_trastive(int **matrix)
{
for (i =0 ; i < lines;i++)
for (j =0;j <colmuns;j++)
for(k = 0;k < colmuns;k++)
{
if ((matrix[i][k] == 1)) &&  (matrix[k][j] ==1)) ** (matrix[i][j] == 0)
{
return false;
}

}
return true;
}```

2. So what's your question? Can you elaborate?

3. soory wrong title it should be [HOWTO] ....

4. Ah, I see. In that case, I'd add an 'i' here:
Code:
```int check_for_antisemtric(int **matrix)
{
for (i =0 ; i < lines;i++)
for (j =0;j <colmuns;j++)
{
if ((matrix[i][j] == 1)) and (matrix[j][i] ==1))
{
return false;
}

}
return true;
}```
And you might want to use && instead of and. and works in C++ and in C when you have iso646.h included, but not in ordinary C. At least mention this if you're going to use and.

Finally, if you're posting code for others to read, consider using consistent indentation, and perhaps code colouring. Which looks better? This?
Code:
```int check_for_Ireflexive(int **matrix)
{
for (i =0 ; i < lines;i++)
{
if (matrix[i][i] == 1) //see the defrence
{
return false;
}
}
return true;
}```
Or this?
Code:
```int check_for_Ireflexive(int **matrix)
{
for (i =0 ; i < lines;i++)
{
if (matrix[i][i] == 1) //see the defrence
{
return false;
}
}
return true;
}```
I syntax highlighted that code with codeform, a program I wrote. Feel free to use it yourself here: http://dwks.theprogrammingsite.com/myprogs/cfonline

5. ## Format for rrelation files

Hello,..

Today i had the exam (secend attempt) and i realized that i didn't posted the relation formats that are used so :
all numbers are Natural numbers(integer or duble) and between two tokens are allways spaces
Code:
`amount_of_pairs x1 y1 x2 y2 x3 y3 ...`
Code:
`amount_of_pairs (x1,y1) (x2,y2) (x3,y3) ...`
and the hardest one
Code:
`(x1,x2) (y1,y2) (x3,y3) ..`
The first should be loaded into an array (two dimentional or one) but be if you use boolean matirx (int **Mr) you should be awere that pairs won't be allways as 1,2 2,3 etc but they can be very large numbers so don't use the next//I acidantly did it :'-( :
Code:
```for (i = 0; i < amount_of_pairs ;i++)
for (j=0;j< amount_of_pairs;j++)
{
fscanf(fp,"(%d,%d)",&k,&s);
Mr[k-1][s-1] = 1;
}```