Advertisement
Ember

Overlap

Mar 27th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. bool Overlap(Int* arrayA, Int* arrayB, Int sizeA, Int sizeB)
  2. {
  3.     // Find out which one is preceeding the other, i.e., uses a smaller address
  4.     if(arrayB < arrayA)
  5.     {
  6.         // Swap the addresses and sizes if B is preceeding A
  7.         Int* tempArray = arrayA;
  8.         arrayA = arrayB;
  9.         arrayB = tempArray;
  10.         //--//
  11.         Int tempSize = sizeA;
  12.         sizeA = sizeB;
  13.         sizeB = tempSize;
  14.     }
  15.  
  16.     // Check if the last element of the preceeding array is below the first element of the proceeding array
  17.     if(&arrayA[sizeA] > &arrayB[0])
  18.     {
  19.         // If so, then part of the first array exists in the second array
  20.         return true;
  21.     }
  22.     else
  23.     {
  24.         // Otherwise, they exist in different areas of memory
  25.         return false;
  26.     }
  27. }
  28.  
  29. bool Overlap(Int* arrayA, Int* arrayB, Int sizeA, Int sizeB)
  30. {
  31.     // Iterate through both arrays
  32.     for(Int j = 0; j < sizeB; ++j)
  33.     {
  34.         for(Int i = 0; i < sizeA; ++i)
  35.         {
  36.             // Check if any of the elements equal another element in the other array
  37.             if(arrayA[i] == arrayB[j])
  38.             {
  39.                 return true;
  40.             }
  41.         }
  42.     }
  43.  
  44.     // If not, then none of the elements overlap
  45.     return false;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement