0

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]<<" ";


}
9
  • 1
    think yourself: what is b[j]? j is a big number. you need to modify the index for the second copying pass. Commented Apr 13, 2012 at 10:10
  • 1
    damn, why do these questions get instant answers b4 I get time to type mine. :( Commented Apr 13, 2012 at 10:12
  • 2
    This code only has a passing resemblance to C++. Learn about standard containers. Commented Apr 13, 2012 at 10:13
  • @IntermediateHacker - Learn to type faster! Commented Apr 13, 2012 at 10:13
  • @Konrad: well, using C-style arrays is perhaps a part of homework assignment. The task is most likely about learning the basic memory allocation techniques, not the Real and Proper C++. Commented Apr 13, 2012 at 10:15

9 Answers 9

5

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].
Sign up to request clarification or add additional context in comments.

1 Comment

[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 ,d=0 ; j < x ; j++,d++) c[j] = b[d]; cout<<endl<<endl; for(int k = 0; k < x; k++) cout<<c[k]<<" "; cout<<endl<<endl;]
2

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);
}

Comments

1

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;
}

Comments

0
c[j] = b[j];

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

Comments

0

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];

1 Comment

it's better not to give the ready-to-use code for homework questions.
0
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;
}

3 Comments

(1) delete[] (2) and what would the OP learn from the ready solution? better is to give some hints.
as it's only a problem related to finding/using the correct index, i think it's okay to give a 'full' solution
then the OP would just paste the code into the assignment form, and forget -- so he would learn nothing from the assignment, thus defeating the whole its purpose. (IMHO)
0

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...

Comments

0
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;
}

Comments

-1
#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 !

5 Comments

and what if nLenA + nLenB > 100? is it impossible to merge the arrays?
assert(0) gets called. It is not impossible but that is not the question, is it ? I think the focus of my solution is to highlight that scenario and that is the reason why I have shown the calling function that ensures that the size of the merged array is greater than the size of the input arrays combined. If we go with memory allocation then the focus of the question shifts from writing 2 for loops to memory management.
@Vlad: So what is your solution ?
dynamic allocation in the solution, of course. it's hard to guess, what is the focus of the assignment -- however, making a deliberate decision of bounding output array size by a constant seems to promote a very bad style for me.
I would prefer to use vector instead of naked pointers / memory allocation to C arrays. Since the merge function has a cout, it is clear that this is not serious programming and the idea here is to learn basic programming constructs such as for loops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.