Concatenate

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

    Concatenate

    Hi there fellas..
    I am learning this powerful language, but came accross a concatenation
    problem that I can't resolve.

    I have 2 arrays

    A1[] = {1,2,3,4};
    A2[] = {1,3,5,7};

    I need to put them together in an int array that looks like this.
    A3[32] = {1,2,3,4,5,7};

    Sum them both, but skip duplicates..

    Can some1 Help Please...
    Thanks...
  • Jakob Bieling

    #2
    Re: Concatenate

    "PerritoPer ron" <mariocrc@ev1.n et> wrote in message
    news:2ca8d45f.0 307041112.7ed7e 52d@posting.goo gle.com...[color=blue]
    > Hi there fellas..
    > I am learning this powerful language, but came accross a concatenation
    > problem that I can't resolve.
    >
    > I have 2 arrays
    >
    > A1[] = {1,2,3,4};
    > A2[] = {1,3,5,7};
    >
    > I need to put them together in an int array that looks like this.
    > A3[32] = {1,2,3,4,5,7};
    >
    > Sum them both, but skip duplicates..[/color]


    Use vectors instead of C-style arrays. Here's some untested snippet:

    vector <int> a1, a2, a3;
    // fill vectors with your values

    a3 = a1; // copy first into a3
    a3.insert (a3.end (), a2.begin (), a2.end ()); // append second
    sort (a3.begin (), a3.end (), less <int> ()); // sort a3
    unique (a3.begin () , a3.end ()); // remove dups

    Again, I just typed this into my newsreader without trying to compile
    this. But it will give you an idea of how it would work.

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)


    Comment

    • Mario Contreras

      #3
      Re: Concatenate

      This is what I have, but it needs a little modification
      It gets all values from 2 arrays, but still not perfect..


      // I need to put 2 arrays together in an
      // single int array.
      // no duplicates


      #include <iostream>
      using namespace std;
      #define L2 << endl << endl
      #define L3 << endl << endl << endl << endl

      class Set{
      public:
      void Sum(int a[], int b[], int c[], int size);
      }; // End of class


      // i Corre a

      void Set::Sum(int a[], int b[], int c[], int size){
      int tmp = size;
      for (int i = 0; i < size; i++){
      c[i] = a[i];
      }

      // Jota corre b

      for (int j = 0; j < size; j++){
      for (int k = 0; k < size; k++){
      if(c[k] != b[j])
      c[j+tmp] = b[j];

      }

      }

      // Just to print all c array

      for (int z = 0; c[z] != '\0'; z++)
      cout << "c[" << z << "] = " << c[z] << endl;

      }

      void main(){
      int size = 4;
      int a[] = {1,2,3,4};
      int b[] = {1,3,5,7};
      int c[32] = {0};
      Set s;
      s.Sum(a, b, c, size);
      }


      Comment

      • John Harrison

        #4
        Re: Concatenate


        "Mario Contreras" <mario@txpos.co m> wrote in message
        news:nXkNa.4104 0$XV.2642469@tw ister.austin.rr .com...[color=blue]
        > This is what I have, but it needs a little modification
        > It gets all values from 2 arrays, but still not perfect..
        >
        >
        > // I need to put 2 arrays together in an
        > // single int array.
        > // no duplicates
        >
        >
        > #include <iostream>
        > using namespace std;
        > #define L2 << endl << endl
        > #define L3 << endl << endl << endl << endl
        >
        > class Set{
        > public:
        > void Sum(int a[], int b[], int c[], int size);
        > }; // End of class[/color]

        Looks like you are trying to write a Set class but you don't really know how
        to design it.

        You need to put one of the arrays inside the Set class, otherwise it's a
        waste of time. Then you need to add some useful member functions to the Set
        class.

        Start like this

        // set, maximum size 32
        class Set
        {
        public:
        Set()
        {
        // make empty set
        }
        Set(int a[], int size)
        {
        // make set from array (remove duplicates)
        }
        void add_int(int val)
        {
        // add one int to array (if not duplicate)
        }
        void union(const Set& x)
        {
        // add another Set to this Set (remove duplicates)
        }
        void print()
        {
        for (int i = 0; i < size; ++i)
        cout << elem[i] << ' ';
        cout << '\n';
        }
        private:
        int elem[32]; // can't have more than 32 integers
        int size; // how many integers we have got
        };

        Then do this

        int main()
        {
        int a[] = {1,2,3,4};
        int b[] = {1,3,5,7};
        Set a_set(a, 4);
        Set b_set(b, 4);
        Set c_set = a_set; // copy a_set to c_set
        c_set.union(b_s et); // add b_set to c_set
        c_set.print();
        }


        All you have to do is write the member functions above.

        john


        Comment

        • John Harrison

          #5
          Re: Concatenate

          > Start like this[color=blue]
          >
          > // set, maximum size 32
          > class Set
          > {
          > public:
          > Set()
          > {
          > // make empty set
          > }
          > Set(int a[], int size)
          > {
          > // make set from array (remove duplicates)
          > }
          > void add_int(int val)
          > {
          > // add one int to array (if not duplicate)
          > }
          > void union(const Set& x)
          > {
          > // add another Set to this Set (remove duplicates)
          > }[/color]

          Of course you can't have a member function called union, change union to
          make_union, or something.

          john


          Comment

          • Chris \( Val \)

            #6
            Re: Concatenate


            "Jakob Bieling" <netsurf@gmy.ne t> wrote in message
            news:be4kkv$obk $07$1@news.t-online.com...
            | "PerritoPer ron" <mariocrc@ev1.n et> wrote in message
            | news:2ca8d45f.0 307041112.7ed7e 52d@posting.goo gle.com...
            | > Hi there fellas..
            | > I am learning this powerful language, but came accross a concatenation
            | > problem that I can't resolve.
            | >
            | > I have 2 arrays
            | >
            | > A1[] = {1,2,3,4};
            | > A2[] = {1,3,5,7};
            | >
            | > I need to put them together in an int array that looks like this.
            | > A3[32] = {1,2,3,4,5,7};
            | >
            | > Sum them both, but skip duplicates..
            |
            |
            | Use vectors instead of C-style arrays. Here's some untested snippet:
            |
            | vector <int> a1, a2, a3;
            | // fill vectors with your values
            |
            | a3 = a1; // copy first into a3
            | a3.insert (a3.end (), a2.begin (), a2.end ()); // append second
            | sort (a3.begin (), a3.end (), less <int> ()); // sort a3
            | unique (a3.begin () , a3.end ()); // remove dups

            [snip]

            Its not that big a deal in this case I suppose, but
            'std::unique()' will *not remove all duplicates*.

            It will only remove *consecutive* duplicates.

            Cheers.
            Chris Val







            Comment

            • Jakob Bieling

              #7
              Re: Concatenate

              "Chris ( Val )" <chrisval@bigpo nd.com.au> wrote
              [color=blue]
              > "Jakob Bieling" <netsurf@gmy.ne t> wrote in message[/color]
              [color=blue]
              > | sort (a3.begin (), a3.end (), less <int> ()); // sort a3
              > | unique (a3.begin () , a3.end ()); // remove dups[/color]
              [color=blue]
              > Its not that big a deal in this case I suppose, but
              > 'std::unique()' will *not remove all duplicates*.
              >
              > It will only remove *consecutive* duplicates.[/color]

              Which is why I used std::sort before removing the dups.

              regards
              --
              jb

              (replace y with x if you want to reply by e-mail)


              Comment

              • Stuart Golodetz

                #8
                Re: Concatenate

                "PerritoPer ron" <mariocrc@ev1.n et> wrote in message
                news:2ca8d45f.0 307041112.7ed7e 52d@posting.goo gle.com...[color=blue]
                > Hi there fellas..
                > I am learning this powerful language, but came accross a concatenation
                > problem that I can't resolve.
                >
                > I have 2 arrays
                >
                > A1[] = {1,2,3,4};
                > A2[] = {1,3,5,7};
                >
                > I need to put them together in an int array that looks like this.
                > A3[32] = {1,2,3,4,5,7};
                >
                > Sum them both, but skip duplicates..
                >
                > Can some1 Help Please...
                > Thanks...[/color]

                #include <algorithm>
                #include <iostream>
                #include <vector>

                int main()
                {
                std::vector<int > vec1(4), vec2(4), vec3;
                vec1[0] = 1;
                vec1[1] = 2;
                vec1[2] = 3;
                vec1[3] = 4;
                vec2[0] = 1;
                vec2[1] = 3;
                vec2[2] = 5;
                vec2[3] = 7;
                vec3.reserve(8) ; // reserve space for the maximum
                possible number of elements necessary
                std::set_union( vec1.begin(), vec1.end(), vec2.begin(), vec2.end(),
                std::back_inser ter<std::vector <int> >(vec3));
                vec3.swap(std:: vector<int>(vec 3)); // shrink-to-fit (if desired)
                std::copy(vec3. begin(), vec3.end(),
                std::ostream_it erator<int>(std ::cout, " "));
                std::cout << '\n';
                return 0;
                }

                There are a number of things to explain about this:

                1) std::vector<int > vec1(4) creates a std::vector (a sort of self-managing
                array) with current size 4.
                2) vec3.reserve(8) ; does *not* resize the vector, it ensures that the
                vector's capacity (distinct from its size) is at least 8. This means that
                the memory has been allocated for at least 8 elements, even though the
                current number of elements (the size of the vector) is still 0.
                3) std::set_union( <blah>) is the bit which does what you want, since what
                you want is in fact just a set union operation.
                4) std::back_inser ter<std::vector <int> >(vec3) is an output iterator which
                pushes things onto the end of vec3.
                5) vec3.swap(std:: vector<int>(vec 3)); trims excess capacity from the vector
                (a so-called "shrink-to-fit" operation). Although the size of vec3 will be 6
                after the set_union, the capacity will still be at least 8. Shrink-to-fit
                attempts to make it as small as possible (whilst still greater than 6),
                though the exact capacity afterwards depends on the implementation.
                6) std::copy(<blah >) outputs the elements of the vector to std::cout, using
                an ostream_iterato r which separates the elements with a space (" ").

                While we're using lots of stuff from the Standard Library, here's a book
                recommendation:

                The C++ Standard Library (Josuttis)

                HTH,

                Stuart.


                Comment

                • Bruce

                  #9
                  Re: Concatenate

                  In comp.lang.c++
                  mariocrc@ev1.ne t (PerritoPerron) wrote:
                  [color=blue]
                  >I am learning this powerful language, but came accross a concatenation
                  >problem that I can't resolve.
                  >
                  >I have 2 arrays
                  >
                  >A1[] = {1,2,3,4};
                  >A2[] = {1,3,5,7};
                  >
                  >I need to put them together in an int array that looks like this.
                  >A3[32] = {1,2,3,4,5,7};
                  >
                  >Sum them both, but skip duplicates..[/color]

                  You have presented the problem but none of your work at solving it. How do
                  YOU think you should do it? Perhaps: sort both, add, compare, add,
                  compare....

                  Comment

                  • Bruce

                    #10
                    Re: Concatenate

                    In comp.lang.c++
                    "Chris \( Val \)" <chrisval@bigpo nd.com.au> wrote:
                    [color=blue]
                    >| sort (a3.begin (), a3.end (), less <int> ()); // sort a3
                    >| unique (a3.begin () , a3.end ()); // remove dups
                    >
                    >[snip]
                    >
                    >Its not that big a deal in this case I suppose, but
                    >'std::unique() ' will *not remove all duplicates*.
                    >
                    >It will only remove *consecutive* duplicates.[/color]

                    Hmmm, you sure about that? If it is sorted first, as it is in his code, it
                    should remove all dupes.

                    "Every time a consecutive group of duplicate elements appears in the range
                    [first, last), the algorithm unique removes all but the first element."

                    Comment

                    Working...