hussain alwazzan
hussain alwazzan

Reputation: 1

Copying two array into one larger array?

I have the following question:

Write a program with a function named "merge" that copies the data integers of one array into a larger sized array, and then copies the data integers of the second array into the larger array just after the contents of the first array

There is something wrong with my function

If I entered {1,2} for array 1 and {3,4} for array 2

then the output is 1 2 -57574 -658675
It should be 1 2 3 4

void merge (int a[], int n, int b[],int m) {

int c[100];
int x=n+m ; //size of merge aray c[] 

for(int i = 0; i < n; i++)
c[i]=a[i];

for(int j = n ; j < x ; j++)
c[j] = b[j];

cout<<endl<<endl;


for(int k = 0; k < x; k++)

cout<<c[k]<<" ";


}

Upvotes: 0

Views: 6009

Answers (9)

ThanhNguyen
ThanhNguyen

Reputation: 21

This is my simple code. Modify it to reach your merge function purpose.

int main() {
    int a[5];
    int b[5];
    int c[10];

    cout << "Enter elements for array a[5]:" << endl;
    int i = 0;
    do {
        cin >> a[i];
        i++;
    } while (i <= 4);

    cout << "Enter elements for array b[5]:" << endl;
    i = 0;
    do {
        cin >> b[i];
        i++;
    } while (i <= 4);

    for (register int x = 0; x <= 5; x++) {
        if (x == 5) {
            for (register int h = 0; h < 5; h++) {
                c[x] = b[h];
                x++;
            }
            break;
        }
        c[x] = a[x];
    }

    for (register int x = 0; x < 10; x++) {
        cout << c[x] << " ";
    }
    return (0);
}

Upvotes: 2

Tadeusz Kopec for Ukraine
Tadeusz Kopec for Ukraine

Reputation: 12403

void merge (int a[], int n, int b[],int m) 
{

    int c = new int[n + m];
    std::copy(a, a + n, c);
    std::copy(b, b + m, c + n);

    cout<<endl<<endl;
    for(int k = 0; k < n+m; k++)
        cout<<c[k]<<" ";    

    delete[] c;
}

Upvotes: 0

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52107

Why not just use vectors? Something like this:

std::vector<int> concat(const std::vector<int>& a, const std::vector<int>& b) {
    std::vector<int> c;
    c.reserve(a.size() + b.size());
    c.insert(c.end(), a.begin(), a.end());
    c.insert(c.end(), b.begin(), b.end());
    return c;
}

Upvotes: 1

Ram
Ram

Reputation: 3133

#include<iostream>
#include<assert.h>


void merge(int first[], int nLenFirst, int second[], int nLenSecond, int merged[]) 
{
    int nTotal = nLenFirst + nLenSecond;

    for(int i= 0; i < nLenFirst; ++i)
        merged[i] = first[i];

    for(int i= nLenFirst, j = 0; i < nTotal; ++i,++j)
        merged[i] = second[j];

}

void main()
{
    int a[] = {2, 4, 5, 7};
    int b[] = {3, 7, 11, 19, 25};

    int nLenA = sizeof(a)/sizeof(int);
    int nLenB = sizeof(b)/sizeof(int);

    int c[100] = {0};

    int nTotal = nLenA + nLenB;
    assert(sizeof(c)/sizeof(int) >= nTotal);

    merge(a, nLenA, b, nLenB, c);

    for(int i = 0; i < nTotal; ++i)
    {
        std::cout << c[i] << std::endl;
    }
}

Focus on the assert !

Upvotes: -1

Rahul Raj
Rahul Raj

Reputation: 11

The inner loop can be rewrite as:

for(int j=0;k=n;j<m,k<x;j++,k++)

{

     c[k]=b[j];

}

I hope youu got the point...

Upvotes: 0

TheVoidSeeker
TheVoidSeeker

Reputation: 144

void merge (int a[], int n, int b[],int m) 
{
  int* c = new int[n+m];

  for(int i = 0; i < n; i++)
    c[i]=a[i];

  for(int j = 0 ; j < m ; j++)
    c[n+j] = b[j]; // <-- there was your fault

  cout<<endl<<endl;
  for(int k = 0; k < n+m; k++)
    cout<<c[k]<<" ";

  delete [] c;
}

Upvotes: 0

Pheonix
Pheonix

Reputation: 6052

in second loop

for(int j = n ; j < x ; j++)
c[j] = b[j];    <---- b[j] not defined, you need to start from b[0]

Try this:

for(int j = n ; j < x ; j++)
c[j] = b[j-n];

Upvotes: 0

Delan Azabani
Delan Azabani

Reputation: 81384

c[j] = b[j];

is the problem here. The first j is correct, but the second j should really be j - n.

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 59997

Problems:

  1. You need to dynamically create the array of the right size - might be more that 100 items.
  2. You need to start copying from b[0] not b[n].

Upvotes: 5

Related Questions