### C++ Problem

Pages: 12
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:

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:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````#include using namespace std; void sortMerge(int n, int *a){ void merge(int *a,int p,int *b,int q, int *c,int n); 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

When I try to run the programm , it shows an error.

undefined reference to winmain.

Can somebody please tell me what I am doing wrong and perhaps help me.
Last edited on
Hello all experts around. Can somebody tell me why my programm is not working?

Iam confused at the moment.

At the very least, you need to
1. replace
 ``12`` `````` delete b; delete c;``````
with
 ``12`` `````` delete[] b; delete[] c;``````

2. add a main function, for example
 ``12345`` ``````int main() { int a[] = {11, 2, 9, 7, 8, 3, 2}; sortMerge(7, a); }``````

3. Set up your project to build a console application, so that it stops looking for winmain
Last edited on
You have no entrypoint. Where should your program start from if you have no main/winmain ?
My Programm is now looking so :

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````#include using namespace std; int main(){ void sortMerge(int n, int *a){ void merge(int *a,int p,int *b,int q, int *c,int n); 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]; } // Arrays einzeln rekursiv sortieren sortMerge(n/2, b); sortMerge(n-n/2, c); // Arrays b und c zusammenfügen merge(b,n/2,c,n-n/2,a,n); // Arrays b und c wieder löschen 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

Why is the programm still not working?

I don`t understand this.

You cannot declare functions inside other functions, like you do in line 6 and 7.

Put these before main().
 ``12`` ``````void sortMerge(int n, int *a){ void merge(int *a,int p,int *b,int q, int *c,int n);``````

Last edited on
Hello I corrected my programm a little:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````#include using namespace std; void sortMerge(int n, int *a){ void merge(int *a,int p,int *b,int q, int *c,int n); int main(){ 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]; } // Arrays einzeln rekursiv sortieren sortMerge(n/2, b); sortMerge(n-n/2, c); // Arrays b und c zusammenfügen merge(b,n/2,c,n-n/2,a,n); // Arrays b und c wieder löschen 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

But I really don´t understand what I should do now?

Can somebody tell me how my implementation should be?
You're going too fast.
You don't know what int main() is and you're already writing functions.
Try with a simpler example next time, begin here: http://www.cplusplus.com/doc/tutorial/
Yeah haha.I also think so but can you Tell me what is wrong in my Code please because I am Görings Mad with this task.
Can somebody tell what I should I correct in my programm?

Because my programm is still not working.
Why are you asking what is wrong and do not read compiler error messages?!!! It is the compiler that says what is wrong with your program.
My Compiler is telling that there is a Problem with One bracket. But i dont find the mistakes. Thats why I am asking.
Can only somebody Tell me how I can correct this Problem?
I am getting the error } before token.

Where is my mistake?
In previous program, compiler tells that in line "if (n>1)", "n" is undeclared.

Assign any value to array a, and assign the size of array "a" to n:

using namespace std;

int a[] = {11, 2, 9, 7, 8, 3, 2};
int n = 7;

void sortMerge(int n, int *a); // (deleted {)
void merge(int *a,int p,int *b,int q, int *c,int n);

int main(){

erase last }

Now builder tells: you are missing to define mergesort().

yet to resolve the recursion ...

I modified your program like this:
#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];
}
// Arrays einzeln rekursiv sortieren
sortMerge(n/2, b);
sortMerge(n-n/2, c);
// Arrays b und c zusammenfügen
merge(b,n/2,c,n-n/2,a,n);
// Arrays b und c wieder löschen
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] << " ";
system("PAUSE");
return 0;
}
Last edited on
post erased
Last edited on
erased
Last edited on
erased
Last edited on
Sorry People your Forum postet my Comment 3 Times.
Last edited on
Pages: 12