Task C++

Hello people I have only a little problem with this task.

I think I have solved the rask right but I am not sure perhaps all experts here can help me:

Task:

It is a function to be created, which by an array a with the method "Sort
Mix "ascending.
Function using recursion: sortMerge (n, a)
Here the procedure is as follows:
If an item contains a maximum of one is finished (already sorted)
otherwise
Create 2 matching arrays b and c
Train 2 halves of the array and save it in a new array b and c
Note:
a new array of names h and m elements can be generated as follows:
double * h = new double [m];
The type is double; other similar types;
m is an expression / a variable of type int
With delete [] h, you can delete it again.
Sort b and c with SortMerge
Mix b and c with the function of internship 9 and store the result in a
Delete b and c.

Proceed to creating a suitable program to test the function.
Example of a step in the algorithm:
Sort is a: 11 2 9 7 8 3 2 with 7 elements.
In 2 parts disassemble and store in b and c: b: 11 2 9 c: 7 8 3 2
The 2 parts with SortMerge by: b: 2 9 11 c: 2 3 7 8
The 2 parts to a mix a: 2 2 3 7 8 9 11


My Programm:

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
#include <iostream>
using namespace std;

int a[] = {11, 2, 9, 7, 8, 3, 2};
int n = 7;
void merge(int *a,int p,int *b,int q, int *c,int n);
void sortMerge(int n, int *a) {
if (n>1) {
   int *b = new int[n/2];
   int *c = new int[(n + 1)/2];
   int i;
for(i=0; i < n/2; i++) {
     b[i] = a[i];
}
for(i=n/2; i < n; i++) {
     c[i - n/2] = a[i];
}

sortMerge(n/2, b);
sortMerge(n-n/2, c);

merge(b,n/2,c,n-n/2,a,n);

delete[] b;
delete[] c;
}
}
void merge(int *b,int p,int *c,int q, int *a,int n) {
    int i=0,j=0,k=0;
while(i<p && j<q) {
  if(b[i]<=c[j]) {
      a[k]=b[i];
       i++;
} else {
    a[k]=c[j];
    j++;
}
k++;
}
if(i==p) {
while(j<q) {
a[k]=c[j];
j++;
k++;
}
} else {
while(i<p) {
a[k]=b[i];
i++;
k++;
}
}
}
int main(){
  sortMerge(7, a);
  for (int i=0; i<n; i++) cout << a[i] << " ";

return 0;
}



Is this programm so ok?

And can please someone tell me how I can modify the program so that when I run the programm I can test the function?

For example when I run I have to type numbers and it shows the numbers sortet like its written in the task?
Can somebody please help me?
Is here a good programmer or some experts who can help me?
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
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
using namespace std;

void merge(int *a,int p,int *b,int q, int *c,int n);
void sortMerge(int n, int *a);
	
void sortMerge(int n, int *a)
{
	if (n>1)
	{
   		int *b = new int[n/2];
   		int *c = new int[(n + 1)/2];
   		int i;
		for(i=0; i < n/2; i++)
		{
     		b[i] = a[i];
		}
		for(i=n/2; i < n; i++)
		{
     		c[i - n/2] = a[i];
		}

		sortMerge(n/2, b);
		sortMerge(n-n/2, c);

		merge(b,n/2,c,n-n/2,a,n);

		delete[] b;
		delete[] c;
	}
}

void merge(int *b,int p,int *c,int q, int *a,int n)
{
    int i=0,j=0,k=0;
    
	while(i<p && j<q)
	{
  		if(b[i]<=c[j])
		{
      		a[k]=b[i];
       		i++;
		}
		else
		{
    		a[k]=c[j];
    		j++;
		}
		k++;
	}
	if(i==p)
	{
		while(j<q)
		{
			a[k]=c[j];
			j++;
			k++;
		}
	}
	else
	{
		while(i<p)
		{
			a[k]=b[i];
			i++;
			k++;
		}
	}
}

int main()
{
	int n,i;
	cout<<"How many items are in your function?  ";
	cin>>n;
	
	int a[n];
	
	for(i=0;i<n;i++)
	{
		cout<<"Item number "<<i+1<<" :  ";
		cin>>a[i];
	}
	
	sortMerge(n, a);
	
  	for (i=0; i<n; i++)
	{
		cout << a[i] << " ";
	}
}


You need double, so change all the integers to doubles. (When I tried changing the ints to doubles, I got a whole lot of errors (more errors coming from the ones I fixed).) Good luck, hope this helps.
This was also my problem , that^s why I was asking.

Perhaaps somebody else can help me.

Otherwise I will let the programm how it is.
Hi there,

If you get compiler errors, please include them in the topic, that makes it easier for us to verify what is wrong with the program.

All the best,
NwN
Do you have an idea how I can change my Programm . Perhaps you can Post another Code.
Because the task is or so already solved.
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
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
using namespace std;

void merge(double *b,int p,double *c,int q, double *a,int n);
void sortMerge(int n, double *a);
	
void sortMerge(int n, double *a)
{
	if (n>1)
	{
   		double *b = new double[n/2];
   		double *c = new double[(n + 1)/2];
   		int i;
		for(i=0; i < n/2; i++)
		{
     		b[i] = a[i];
		}
		for(i=n/2; i < n; i++)
		{
     		c[i - n/2] = a[i];
		}

		sortMerge(n/2, b);
		sortMerge(n-n/2, c);

		merge(b,n/2,c,n-n/2,a,n);

		delete[] b;
		delete[] c;
	}
}

void merge(double *b,int p,double *c,int q,double *a,int n)
{
    int i=0,j=0,k=0;
    
	while(i<p && j<q)
	{
  		if(b[i]<=c[j])
		{
      		a[k]=b[i];
       		i++;
		}
		else
		{
    		a[k]=c[j];
    		j++;
		}
		k++;
	}
	if(i==p)
	{
		while(j<q)
		{
			a[k]=c[j];
			j++;
			k++;
		}
	}
	else
	{
		while(i<p)
		{
			a[k]=b[i];
			i++;
			k++;
		}
	}
}

int main()
{
	int n,i;
	cout<<"How many items are in your function?  ";
	cin>>n;
	
	double a[n];
	
	for(i=0;i<n;i++)
	{
		cout<<"Item number "<<i+1<<" :  ";
		cin>>a[i];
	}
	
	sortMerge(n, a);
	
  	for (i=0; i<n; i++)
	{
		cout << a[i] << " ";
	}
}


OMG I FIXED IT!!!!!!!!!!!!!!!!!!!!!!!!! IT WORKS! YOUR'E WELCOME!!! ;D ;D :D :) ;)
Topic archived. No new replies allowed.