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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>

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"<<endl;
    for(int t=0;t<2; t++)
    {
    cout<<t<<":";
      cin>>arra[t];
    }
    cout <<"enter the numbers in your second array (has have 2(two)numbers"<<endl;
    for(int t=0;t<2; t++)
    {
    cout<<t<<":";
      cin>>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<<merg[o]<<", ";
    }
      int ary [4];
      ary[0]=merg[0];
      ary[1]=merg[1];
      ary[2]=merg[2];
      ary[3]=merg[3];
    int swp [4];
    if (ary [0]<ary [1])
    {
        swp [0]=ary [0];
        swp [1]=ary [1];
    }
    if (ary [0]>ary [1])
    {
        swp [0]=ary [1];
        swp [1]=ary [0];
    }
    if (swp [1]<swp [2])
    {
        swp [2]=ary [2];
    }
    if (swp [1]>ary [2])
    {
        swp [1]=ary [2];
        swp [2]=ary [1];

    }
    if (swp [2]<ary [3])
    {
        swp [3]=ary [3];
            for (int i=0;i<4;i++)
    {
        cout << swp [i]<< ",";
    }
    }
    if (swp [2]>ary [3])
    {
        swp [2]=ary [3];
        swp [3]=ary [2];
                for (int i=0;i<4;i++)
    {
        cout << swp [i]<< ",";
    }
    }
    cout<<""<<endl;
        for (int i=0;i<4;i++)
    {
        cout << swp [i]<< ",";
    }
    return 0;
}


Thanks
after line 34 :

1
2
3
4
5
6
7
8
9
10
11
12

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

//to display elements...
for(int i =0;i<4;i++){
std::cout<<ary[i];
}

Look at using a bubble sort.

1
2
3
4
5
6
7
8
9
10
11

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

1
2
3
4
5
6
7
8
9
int i=0,help;
while(i<length-1){
	if(arra[i]>arra[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??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>

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"<<endl;
    for(int t=0;t<2; t++)
    {
    cout<<t<<":";
      cin>>arra[t];
    }
    cout <<"enter the numbers in your second array (has have 2(two)numbers"<<endl;
    for(int t=0;t<2; t++)
    {
    cout<<t<<":";
      cin>>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<<merg[o]<<", ";
    }
      int ary [4];
      ary[0]=merg[0];
      ary[1]=merg[1];
      ary[2]=merg[2];
      ary[3]=merg[3];
    int swp [4];
 int i=0,help;
 int length;
 while(i<length-1){
	if(arra[i]>arra[i+1]){
		help=arra[i];
		arra[i]=arra[i+1];
		arra[i+1]=help;
	}
	i++;
    for (int t=0; t<4; i++)
    {
     cout<<arra[t]<<",";
    }
}
    return 0;
}


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
1
2
3
4
merg[0]=arra[0];
merg[1]=arra[1];
merg[2]=num[0];
merg[3]=num[1];

Is hard coding.
1
2
3
4
5
6
for(int i=0; i<arrasize; ++i){
    merg[i]=arra[i];
}
for(int i=0; i<numsize; ++i){
    merg[i]=num[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?
Registered users can post here. Sign in or register to post.