Extracting arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Umesh

    Extracting arrays

    How can I extract common elements of two number arrays into another
    array?

    // the following doesn't work
    for(k=0;k<10;k+ +)
    for(l=0;l<15;l+ +)
    if(p[k]==q[l]) {r[m]=p[k];++m;}

  • Richard Tobin

    #2
    Re: Extracting arrays

    In article <1181912130.559 391.319460@i13g 2000prf.googleg roups.com>,
    Umesh <fraternitydisp osal@gmail.comw rote:
    >How can I extract common elements of two number arrays into another
    >array?
    >
    >// the following doesn't work
    for(k=0;k<10;k+ +)
    > for(l=0;l<15;l+ +)
    > if(p[k]==q[l]) {r[m]=p[k];++m;}
    >
    What doesn't work about it?

    You need to show us a complete program.

    -- Richard

    --
    "Considerat ion shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.

    Comment

    • osmium

      #3
      Re: Extracting arrays

      "Umesh" writes:
      How can I extract common elements of two number arrays into another
      array?
      >
      // the following doesn't work
      for(k=0;k<10;k+ +)
      for(l=0;l<15;l+ +)
      if(p[k]==q[l]) {r[m]=p[k];++m;}
      Congratulations Umesh! You finally figured out that the question belongs in
      the message. The code fragment you posted doesn't show any means of starting
      m at 0. Does the real code take care of that?


      Comment

      • gw7rib@aol.com

        #4
        Re: Extracting arrays

        On 15 Jun, 13:55, Umesh <fraternitydisp o...@gmail.comw rote:
        How can I extract common elements of two number arrays into another
        array?
        >
        // the following doesn't work
        for(k=0;k<10;k+ +)
        for(l=0;l<15;l+ +)
        if(p[k]==q[l]) {r[m]=p[k];++m;}
        This code looks OK to me, as long as you set m to zero beforehand and
        r is big enough. The one snag I can see is that, if an element occurs
        more than once in a list, and at least once in the other, it may be
        selected in r more times than you want. One way round that might be to
        change the value in p to some sort of non-value (eg you could use 0,
        or -1, if this wasn't going to occur in the real data). An alternative
        might be to have another array to indicate whether the value in p had
        been "used" yet or not. It's up to you.

        Hope this helps.
        Paul.

        Comment

        • Ben Bacarisse

          #5
          Re: Extracting arrays

          gw7rib@aol.com writes:
          On 15 Jun, 13:55, Umesh <fraternitydisp o...@gmail.comw rote:
          >How can I extract common elements of two number arrays into another
          >array?
          >>
          >// the following doesn't work
          > for(k=0;k<10;k+ +)
          > for(l=0;l<15;l+ +)
          > if(p[k]==q[l]) {r[m]=p[k];++m;}
          >
          This code looks OK to me, as long as you set m to zero beforehand and
          r is big enough. The one snag I can see is that, if an element occurs
          more than once in a list, and at least once in the other, it may be
          selected in r more times than you want. One way round that might be to
          change the value in p to some sort of non-value (eg you could use 0,
          or -1, if this wasn't going to occur in the real data). An alternative
          might be to have another array to indicate whether the value in p had
          been "used" yet or not. It's up to you.
          That "other array" already exists (r). If I were forced to use this
          rather inefficient method, I'd write a "membership " function:

          int in(int x, int p[], int l)
          {
          /* Return 1 if x is equal to any element in p[0]..p[l-1]; 0 otherwise */
          int k;
          for (k = 0; k < l; k++)
          if (x == p[k])
          return 1;
          return 0;
          }

          and then do something like:

          for (k = 0; k < sz1; k++)
          if (!in(p[k], r, m) && in(p[k], q, s2))
          r[m++] = p[k];

          A much better way, is to sort p and q and the do a sort of
          "anti-merge". That will be much faster for large arrays.

          --
          Ben.

          Comment

          • Umesh

            #6
            Re: Extracting arrays

            Count no. of same elements in an number array and then display the
            result.

            Comment

            • deepak

              #7
              Re: Extracting arrays

              On Jun 16, 7:34 pm, Umesh <fraternitydisp o...@gmail.comw rote:
              Count no. of same elements in an number array and then display the
              result.
              using this code if same number is there in any of the array more than
              once,
              it will copy again. Is it really needed?

              Comment

              • Chris Dollin

                #8
                Re: Extracting arrays

                Umesh wrote:
                Count no. of same elements in an number array and then display the
                result.
                I get `17`.

                --
                Chris "DYOH" Dollin

                Hewlett-Packard Limited Cain Road, Bracknell, registered no:
                registered office: Berks RG12 1HN 690597 England

                Comment

                Working...