Determine if a relation is symmetric

Hello all I am having trouble writing a function that will determine if a relation is symmetric. Any tips as to what I am doing wrong would be greatly appreciated. The arr arrays are the relations, arm arrays are the unique number sin each relation.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 int arr[]  =  {1,1,2,2,3,3,4,4,5,5};
int arm[]  =  {1,2,3,4,5};
int arr2[] =  {1, 1, 2, 2, 3, 3, 4, 4, 4, 7, 7, 4, 7, 7};
int arm2[] =  {1,2,3,4,7};
int arr3[] =  {1, 1, 2, 4, 3, 9, 4, 16, 5, 25, 6,36, 7, 49, 8, 64};
int arm3[] =  {1,2,3,4,9,16,5,25,6,36,7,49,8,64};

int n   = sizeof(arm)/sizeof(int);
int nn  = sizeof(arr)/sizeof(int);
int n2  = sizeof(arm2)/sizeof(int);
int nn2 = sizeof(arr2)/sizeof(int);
int n3  = sizeof(arm3)/sizeof(int);
int nn3 = sizeof(arr3)/sizeof(int);


bool pair_exist(int left, int right, int b[], int nn)
{
   for (int i=0; i<nn; i+=2)
   {
      if (left==b[i] && right==b[i+1])  return true;
   }

   return false;
}


  bool symmetric(int arr[],int nn)
{
   for(int i=0;i<nn;i+=2)
    {
     int e = arr[i];
     int f = arr[i+1];
       cout<<"f " <<f<<endl;
       cout<<"e " <<e<<endl;

      if(!pair_exist(e,f,arr,nn))
          return false;
    }
return true;
}

You can't sizeof an array in c++... if you try, it will jus return the size of the pointer and not the array.

If you want to use size of, use vectors
Last edited on
Any tips as to what I am doing wrong would be greatly appreciated.

Could you expand on that (what is happening and what is expected to happen)?

It appears that you go out of bounds here:
if (left==b[i] && right==b[i+1]) return true;
and here:
int f = arr[i+1];
When i = nn-1

You can't sizeof an array in c++... if you try, it will jus return the size of the pointer and not the array.

Nope. If you use sizeof on a pointer, it will return the size of a pointer:

http://ideone.com/P51Osj
Topic archived. No new replies allowed.