merge

I have to use a function to merge two string in a increasing matter
the user gives the number of elements of first array and then it's elements and same for second array.
then a function should sort it.here is an example.
3(n in below)
-1 4 7(array a)
4(m in below)
2 4 9 12(array b)
-1 2 4 4 7 9 12(the answer I should get)
my code has errors I don't know how to solve that's why I'm asking this question.

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
 #include<iostream>
using namespace std;
int* merge(int*a, int*b);
//I'm not allowed to change anything in the function on top
int main(){
int a[100],b[100],n,m,i,j;
cin>>n;
for(i=0;i<n;i++){
	cin>>a[i];
}
//user gives a number that is how long is the fuction
// and then gives the function	
cin>>m;
for(i=0;i<m;i++){
	cin>>b[i];
}
//same thing as top	
cout<< *merge(*a,*b);
//I'm not sure if the line on top is working right.
	
	return 0;
}
int* merge(int*a, int*b){
int i,j=0,k=0,x[200];
//this is the function but is it right to use *a and *b in this way?
for(i=0;i<200;i++){
	if(a[j]<b[k]){
		x[i]=a[j];
		j++;
	}
	if(a[j]>b[k]){
		x[i]=b[k];
		k++;
	}
	if(a[j]==b[k]){
		x[i]=a[j];
		x[i+1]=b[k];
		k++;
		j++;
		i++;
	}	
}	
return *x;
//is the return right?	
}
Last edited on
Hello parinaz mellatdoust,

This may help until I can get into the program more after lunch.

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
#include<iostream>

using namespace std;

int* merge(int* a, int* b);
//I'm not allowed to change anything in the function on top

int main()
{
	int a[100]{}, b[100]{}, n{}, m{}, i{}, j{};  // <--- Initialize your variables.

	cin >> n;

	for (i = 0; i<n; i++)
	{
		cin >> a[i];
	}

	//user gives a number that is how long is the function
	// and then gives the function	
	cin >> m;

	for (i = 0; i <m  ; i++)
	{
		cin >> b[i];
	}

	//same thing as top	
	cout << *merge(*a, *b);  // <--- The prototype is correct the function call is not. All you need is
	                         // merge(a, b) here. Even that will not print the array.

	//I'm not sure if the line on top is working right.

	return 0;
}

int* merge(int* a, int* b)
{
	int i{}, j = 0, k = 0, x[200]{};

	//this is the function but is it right to use *a and *b in this way?
	for (i = 0; i < 200; i++)
	{
		if (a[j]<b[k])
		{
			x[i] = a[j];
			j++;
		}

		if (a[j]>b[k])
		{
			x[i] = b[k];
			k++;
		}

		if (a[j] == b[k])
		{
			x[i] = a[j];
			x[i + 1] = b[k];
			k++;
			j++;
			i++;
		}
	}
	return *x;
	//is the return right?
	//  No. You are dereferencing "x" ot returning a pointer.
	//  Also the array "x" will be lost when the function ends. Making the array "x" static should solve any
	//  problems.
}


Notice the blank lines and spaces I used. It makes the program easier to read.

You may find this of some use: http://www.cplusplus.com/doc/tutorial/pointers/

Hope that helps for now,

Andy
Last edited on
I tried to do as you said and make it static it's the only problem I have now but I tried a few methods and they didn't work can you give me a hint about how to do so.
also thanks for the tip
Hello parinaz mellatdoust,

First off you made changes to your original program. It would help if you post the new code so I can see what you have done and if you missed anything.

When I got the program to compile and run I was faced with a blank window and a flashing cursor having no idea what to enter. As a start here is a suggestion. You can change the cout statements to whatever you like:

1
2
3
4
5
6
7
8
9
std::cout << "\n How many numbers do you want to use? 1 to 100: ";
cin >> n;
std::cout << std::endl;  // <--- Just adds a blank line to the output.

for (i = 0; i < n; i++)
{
	std::cout << " Enter number " << i + 1 << " of " << n << ": ";
	cin >> a[i];
}

This same concept can work for the second input of "m" and the for loop that follows.

A thought for the future and now if you want. Above main define something like

1
2
3
constexpr int MAXSIZEA{ 100 };
constexpr int MAXSIZEB{ 100 };
constexpr int MAXSIZEX{ 200 };


This is one rare time I suggest using a global variable only because its value can not be changed anywhere in the program. If "constexpr" should be a problem for you than you can just use "const". BTW the capital letters just help to let you know that the variable is defined as a const. Now you can use these variables anywhere in the program and only have to change the value in one place if you need to.

The last part of main is cout<< *merge(*a,*b);. First this will not print the entire array only the dereferenced value of that address returned by "merge". What you will need to do is define an int pointer in main and set the return value of "merge" to this pointer. Then you can use a for loop to print the array.

Now for the "merge" function:

The first thing that you do is define "int i". This is unnecessary because you can easily difine this in the for loop as for (int i = 0; i < 200; i++). The only reason to define outside the for loop is to use this variable before the function ends and outside the for loop.

Defining the array "x" as "static" is necessary to retain the array after the function ends.

The if statements do not work as you have them. I am thinking they should be if/else if statements, but even this may not work. Not sure as I have not tried this yet.

It may be better to sort arrays "a" and "b" separately then add each individual array to "x". The next problem here is that you have no idea how much of "a" and "b" have been used. The variables "n" and "m" are in main and the function can not see these values. This is another rare case where a global variable would be useful. Otherwise you could easily exceed the upper boundary of the arrays. A good example of the afore mentioned "MAXSIZEA" and "MAXSIZEB" being useful.

Given the output that you want it would still be better to sort each array first then merge the two arrays together with your if/else if statements. As it is you are assuming that arrays "a" and "b" are in sorted order. This may not always be the case and you should as a programmer account for this.

This should give you something to think about and work with while I work more on the program.

Hope that helps,

Andy

Edit:
Last edited on
//I'm not allowed to change anything in the function on top

Do you mean you can't change merge()? If so then you have to make n and m globals because merge() needs to know how big the arrays are.

Line 6: j is unused.
line 42: why 200?

As for the merge function, think of it this way: each pass through the loop should put exactly one item into the output array:
- If you've already put in all the items from a, then put in the item from b
- else if you've already put in all the items from b, then put in an item from a.
- else if the current item from a is smaller then put it in
- else put in the item from b.

Since there are three different indexes going here, I'd name them something that corresponds to their arrays. Maybe ax, bx, and xx.

Regardless of where you get the item, be sure to increment the index to the corresponding array.

Topic archived. No new replies allowed.