Home c search for integer array, si

search for integer array, si

Author

Date

Category

Good afternoon.
Let there be two array of integers:

int arr1 [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, nine};
int arr2 [] = {3, 4, 5};

There is a function (like STRSTR for strings), which will return the pointer if ARR2 enters Arr1, and returns NULL if the entry is not found.


Answer 1, Authority 100%

sketched the forehead:

# include & lt; stdio.h & gt;
#Include & lt; String.h & gt;
INT FIND_SUBARRAY (Const In * A, Size_t An, Const Int * B, Size_T BN)
{
  for (int i = 0; i & lt; = an - bn; i ++)
  {
    if (MemCMP (A + I, B, BN * SizeOF (int)) == 0)
      Return I;
  }
  Return -1;
}
INT MAIN ()
{
  int a [] = {2, 5, 1};
  int b [] = {2, 5, 1};
  INT POS = FIND_SUFARRAY (A, SIZEOF (A) / SIZEOF (INT), B, SIZEOF (B) / SIZEOF (INT));
  IF (POS & GT; = 0)
    PrintF ("B = A [% I,% LU] \ N", POS, POS + SizeOf (B) / SizeOF (int));
  ELSE.
    Printf ("B is not subarray of a \ n");
  Return 0;
}

Answer 2

wrote an option on C++, but there is no significant difference. I would like to hear comments from knowledgeable people as far as correct in terms of the use of data types. Of the advantages of the method – the complexity of o (n) .

# include & lt; iostream & gt;
#Include & lt; vector & gt;
Using Namespace STD;
TypeDef Unsigned Long Long ULL;
Typedef Pair & LT; ULL, ULL & GT; PLL;
Const Ull Base = 31;
Const Pll mod = pll (100000000013.1000000000009);
TEMPLATE & LT; TYPENAME T & GT;
Void Modif (PLL & AMP; Value, T Add) {
  Value = PLL ((Value.first * Base + Add)% mod.first, (Value.second * Base + Add)% mod.second);
}
Template & lt; TypeName Leftoperand, TypeName Rigthoperand & GT;
Leftoperand * Find (vector & lt; leftoperand & gt; & amp; left, vector & lt; rigthoperand & gt; & amp; rigth) {
   if (rigth.size () & gt; left.size ())
    RETURN NULLPTR;
   PLL POWMOD = PLL (1,1);
   PLL CURRENTLEFT = PLL (0,0);
   PLL Valuerigth = PLL (0,0);
   For (Size_t i = 0; i & lt; rigth.size (); i ++) {
     Modif (PowMod, 0);
     Modif (CURRENTLEFT, LEFT [I]);
     Modif (Valuerigth, Rigth [i]);
   }
   FOR (SIZE_T L = 0, R = RIGTH.SIZE ();; L ++, R ++) {
      If (CurrentLeft == Valuerigth)
        Return & amp; left [l];
      if (r == left.size ())
        RETURN NULLPTR;
      Modif (CURRENTLEFT, LEFT [R]);
      currentleft = pll (
        (CurrentLeft.First - Left [L] * Powmod.first% mod.first)% mod.first,
        (CurrentLeft.second- Left [L] * PowMod.second% mod.second)% mod.second
      );
   }
}
INT MAIN () {
  Vector & lt; int & gt; a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  Vector & lt; Short & GT; b = {3, 4, 5};
  COUT & LT; & LT; Find (A, B) - & amp; a [0]; // 3.
  Return 0;
}

Answer 3

Option with standard functions, in my brief and simple.

int f_cmp (const void * a, const void * b) {
  RETURN MEMCMP (A, B, SIZEOF (INT) * 3);
 };
// ----------------------------------
WinAPI WinMain (Hinstance, Hinstance, LPSTR, INT)
{
int arr1 [] = {0,1,2,3,4,5,6,7,8,9};
int arr2 [] = {7,8,9};
unsigned num = ((SizeOF (Arr1) - SizeOF (ARR2)) / SizeOF (int)) + 1;
void * x = lfind (ARR2, ARR1, & amp; Num, SizeOF (int), F_CMP);
if (x) {
  num = ((int) ((char *) x - (char *) arr1)) / SizeOF (int);
  / * NUM * /
} else {
  /*Not found*/
 }
}

Answer 4

# include & lt; stdio.h & gt;
void * sub_find (const void * a, size_t n1, const void * b, size_t n2,
        Size_t Size, Int (* PCMP) (Const Void *, Const void *)) {
  Const unsigned char * p1, * p2, * i, * j, * e1, * e2;
  IF (N1 & LT; N2)
    RETURN NULL;
  P1 = (Const unsigned char *) a;
  E1 = P1 + (N1 * Size);
  P2 = (Const unsigned char *) b;
  e2 = p2 + (N2 * Size);
  For (; P1 & LT; E1; P1 + = Size) {
    I = P1;
    j = p2;
    While ((i & lt; e1) & amp; & amp; (j & lt; e2) & amp; & amp; (* pcmp) (i, j)) {
      I + = Size; 
J + = Size;
    }
    if (j == E2)
      Return (void *) P1;
  }
  RETURN NULL;
}
Int ICMP (Const Void * A, Const Void * B) {
  Return * (int *) a == * (int *) b;
}
INT SCMP (Const Void * A, Const Void * B) {
  Return * (short *) a == * (short *) b;
}
INT DCMP (Const Void * A, Const Void * B) {
  Return * (Double *) a == * (double *) b;
}
INT MAIN (Void) {
  Short * SP;
  int * ip;
  Double * DP;
  int i1 [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  int i2 [] = {3, 4, 5};
  Short S1 [] = {0xa, 0xb, 0xc, 0xd, 0xe, 0xF, 0x8, 0x9};
  Short S2 [] = {0xd, 0xe, 0xF, 0x8};
  Double D1 [] = {1.5, 0.5, 7.9, 3.4, 0.9, 4.7, 100.56, 44.4};
  DOUBLE D2 [] = {0.5, 7.9};
  // for int
  IP = (int *) sub_find (i1, SizeOF (I1) / SizeOF (i1 [0]),
            I2, SizeOF (i2) / SizeOF (I2 [0]), SizeOF (int), & amp; ICMP);
  if (ip! = null) {
     While (IP! = i1 + SizeOf (i1) / SizeOF (I1 [0]))
       Printf ("% d", * IP ++);
     Putchar ('\ n');
  }
  // For Short
  SP = (SHORT *) sub_find (S1, SizeOF (S1) / SIZEOF (S1 [0]),
             S2, SizeOF (S2) / Sizeof (S2 [0]), Sizeof (Short), & amp; SCMP);
  if (SP! = NULL) {
     While (SP! = S1 + SizeOF (S1) / SizeOF (S1 [0]))
       PrintF ("0x% x", * SP ++);
     Putchar ('\ n');
  }
  // For Double
  DP = (Double *) sub_find (D1, SizeOF (D1) / SIZEOF (D1 [0]),
              D2, SizeOF (D2) / SizeOF (D2 [0]), Sizeof (Double), & amp; DCMP);
  If (DP! = NULL) {
     While (DP! = D1 + SIZEOF (D1) / SIZEOF (D1 [0]))
        PrintF ("% lg", * dp ++);
     Putchar ('\ n');
  }
  Return 0;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions