simple merge

Write a function that merges two vectors, alternating elements from both vectors. If one vector is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer vector.

For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the vector
1 9 4 7 9 4 16 9 11


/* Name: Jan Dierekh B. Arroyo
* Date: February 28,2019
* File: PROB65.CPP
* Descp:
*
*/

#include <iostream>
#include <vector>


using namespace std;

vector<int> merge(vector<int> a, vector<int> b);

int main(){



int arr[] = {1, 4, 9, 16};
vector <int> a (arr, arr + sizeof(arr)/sizeof(arr[0]));


int arr1[] = {4, 7, 9, 9, 11};
vector <int> b (arr1, arr1 + sizeof(arr1)/sizeof(arr1[0]));


cout << endl;
cout << endl;

cout << "The First Vector contain: {1, 4, 9, 16} " << endl;
cout << "The Second Vector contain: {4, 7, 9, 9, 11} " << endl;

cout << endl;
cout << endl;



cout << "This us the integers after being merge after the other alternately: " << endl;

vector<int> c = merge (a,b);
int i;

merge(a.begin(), a.end(), b.begin(), b.end(), c.begin());

cout << "Result of merge:\n";

for(i=0; i > c.size(); i++)
{
cout << c[ i ];
}



cout << endl;
cout << endl;

system("Pause");
return 0;


}


vector<int> merge(vector<int> a, vector<int> b)
{
int n = a.size();
int m = b.size();
vector<int> c(n + m);
int i;

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

return c;
}



Please can you give me advice on why this code is not working. It will be a really big help.
Please use code formatting tags. Edit your post and add [code] and [/code] around your code.

It might help others to explain what exactly is wrong with it (how does the result differ from what you expect), without forcing us to run the program ourselves.

1.
1
2
3
4
    for(i=0; i > c.size(); i++)
    {	
        cout << c[ i ];
    }

You're doing i > c.size().
You should be doing i < c.size(), because i starts at 0 and increments.

2.
C++'s std::merge is defined in <algorithm>, so you should #include <algorithm> if you wish to use it.
Or just remove your second call to merge all together, because it sounds like you should only be using your custom merging method, and not std::merge.

Your merge logic is of course not complete either, because nothing is alternating. But hopefully your program will now compile, and you can change it easier now. I also suggest putting spaces in your output.
Last edited on
assuming the 2 lists are the same size:
for(i = 0; i < either.size(); i++)
{
result.push_back(a[i]);
result.push_back(b[i]);
}

its only slightly more complex if you allow the lists to be of different sizes, then you loop over the max of the sizes, and ignore whichever one is out of bounds when that happens.

alternating is easy, but if you wanted to merge sorted lists into a sorted list you need more logic.
Last edited on
Thank you very much @Ganado big help, and I learned a lot, and sorry i'm new to this forum.
Topic archived. No new replies allowed.