### how to merge and order arrays

Hi!I'm a beginner and I'm trying to make a program that merges and sorts arrays and this is what I got so far
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879`` ``````#include using namespace std; int main() { int num [2]; int arra[2]; int merg[4]; cout <<"enter the numbers in your array (has have 2(two)numbers"<>arra[t]; } cout <<"enter the numbers in your second array (has have 2(two)numbers"<>num[t]; } merg[0]=arra[0]; merg[1]=arra[1]; merg[2]=num[0]; merg[3]=num[1]; for(int o=0; o<4;o++) { cout<ary [1]) { swp [0]=ary [1]; swp [1]=ary [0]; } if (swp [1]ary [2]) { swp [1]=ary [2]; swp [2]=ary [1]; } if (swp [2]ary [3]) { swp [2]=ary [3]; swp [3]=ary [2]; for (int i=0;i<4;i++) { cout << swp [i]<< ","; } } cout<<""<

Thanks
after line 34 :

 ``123456789101112`` `````` // this bubble sort ... for(int i=0;i<4;i++){ for(int j=0;j<4-i;j++){ if(ary[j-1]

Look at using a bubble sort.

 ``1234567891011`` `````` // bubble sort in ascending order for (int i = 0; i < 4; i++) for (int j = 0; j < 4 - i - 1; j++) if (merg[j] > merg[j + 1]) { temp = merg[j]; merg[j] = merg[j + 1]; merg[j + 1] = temp; } ``````
Is there any possible way without using any functions or hard coding, but with while loops and for loops
Yes, it is possible.

 ``123456789`` ``````int i=0,help; while(iarra[i+1]){ help=arra[i]; arra[i]=arra[i+1]; arra[i+1]=help; } i++; }``````
Here I assume you are not learning algorithms about sorting.
After copying those elements into merg, write
`std::sort(merg, merg + 4);`
You can find std::sort in header <algorithm>.
Oooh, never heard of it. My way works anyways.
in the while loop how do I put that into my program. Do I do it like this??

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include using namespace std; int main() { int num [2]; int arra[2]; int merg[4]; cout <<"enter the numbers in your array (has have 2(two)numbers"<>arra[t]; } cout <<"enter the numbers in your second array (has have 2(two)numbers"<>num[t]; } merg[0]=arra[0]; merg[1]=arra[1]; merg[2]=num[0]; merg[3]=num[1]; for(int o=0; o<4;o++) { cout<arra[i+1]){ help=arra[i]; arra[i]=arra[i+1]; arra[i+1]=help; } i++; for (int t=0; t<4; i++) { cout<

but that doesn't work, if I type in 2,1,4,3 it shows 2,1,3,4 . if there are two same numbers, then it doesn't work
Last edited on
 ``1234`` ``````merg[0]=arra[0]; merg[1]=arra[1]; merg[2]=num[0]; merg[3]=num[1];``````

Is hard coding.
 ``123456`` ``````for(int i=0; i

Is not.
But that does not concatenate.

@Dexter
You try to sort arra. You have only 2 values in it. Then you print 4 values.
More seriously, what is the value of `length` on line 38?
Topic archived. No new replies allowed.