Correction of Merge

Please can you help me find a errors corrected in this code

//Merge Sort

#include "iostream"
#include <conio.h>
#include <stdio.h>

using namespace std;

{ int main()
int mrg_sort(int arr1[], int n1, int arr2[], int n2, int result[])
{
int i, j, k;
i = j = k = 0;
while (i< n1 && j<n2){
if (arr1[i] < arr2[j])
result[k++] = arr1[i++];
elseif(arr1[i] > arr2[j])
result[k++] = arr2[j++];
else{
result[k++] = arr1[i++];
j++;
}
}
if (i<n1){
while (i<n1)
result[k++] = arr1[i++];
}
elseif(j<n2){
while (j<n2)
result[k++] = arr2[j++];
}

void main(){
int arr1[10], arr2[10], result[20], i;
int clrscr();
cout << "Enter 10 numbers [arr1]: ";
for (i = 0; i<10; i++)
cin >> arr1[i];
cout << "\nEnter 10 numbers [arr2]: ";
for (i = 0; i<10; i++)
cin >> arr2[i];

int item = mrg_sort(arr1, 10, arr2, 10, result);
cout << "\n\nAfter sort : ";
for (i = 0; i<item; i++)
cout << result[i] << " ";



_getch();

}
place the code in code tag

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
//Merge Sort 
 
#include "iostream" 
#include <conio.h> 
#include <stdio.h> 
 
using namespace std; 
 
int mrg_sort(int arr1[], int n1, int arr2[], int n2, int result[]) 
{ 
        int i, j, k; 
        i = j = k = 0; 
        while (i< n1 && j<n2){ 
                if (arr1[i] < arr2[j]) 
                        result[k++] = arr1[i++]; 
                elseif(arr1[i] > arr2[j]) 
                        result[k++] = arr2[j++]; 
                else{ 
                        result[k++] = arr1[i++]; 
                        j++; 
                } 
        } 
        if (i<n1){ 
                while (i<n1) 
                        result[k++] = arr1[i++]; 
        } 
        elseif(j<n2){ 
                while (j<n2) 
                        result[k++] = arr2[j++]; 
        } 
} 
void main(){ 
        int arr1[10], arr2[10], result[20], i; 
        int clrscr(); 
        cout << "Enter 10 numbers [arr1]: "; 
        for (i = 0; i<10; i++) 
                cin >> arr1[i]; 
        cout << "\nEnter 10 numbers [arr2]: "; 
        for (i = 0; i<10; i++) 
                cin >> arr2[i]; 
 
        int item = mrg_sort(arr1, 10, arr2, 10, result); 
        cout << "\n\nAfter sort : "; 
        for (i = 0; i<item; i++) 
                cout << result[i] << " "; 
 
 
 
        _getch(); 
 
}


What are the errors? logical? compilation? (I haven't tried to compile)
I have corrected obvious error of mismatched brackets and two main functions.
Last edited on
Its logical and compilation

I just used this code and its bringing up errors
Yes it would. I haven't fixed all your errors just one, mismatched brackets

You need to specifically mention which errors you are not able to solve, so that forum members do not have to explicitly spend their time on building, solving errors and posting back.
Errors

Lines 19-20: you're dropping an element.

    (2 4 6) + (2 3 5) --> (2 3 4 5 6)

What this means is that the 'if' should only have two branches: either you take from arr1 or you take from arr2.

Line 31: you forgot to return k

Suggestions

Lines 23,26,27,30 are unnecessary.

Line 34 -- I think you meant to invoke the function, right? (You want the screen cleared?) Right now you have declared a function prototype. Get rid of 'int'.

Hope this helps.
Syntax errors, illegal else without matching if, issuing function header (old-style formal list), and else if: identifier not found are the errors from the code below. I would appreciate a correction of these codes.


//Merge Sort

#include "iostream"
#include <conio.h>
#include <stdio.h>

using namespace std;

int mrg_sort(int arr1[], int n1, int arr2[], int n2, int result[])
{
int i, j, k;
i = j = k = 0;
while (i< n1 && j<n2){
if (arr1[i] < arr2[j])
result[k++] = arr1[i++];
elseif(arr1[i] > arr2[j])
result[k++] = arr2[j++];
else{
result[k++] = arr1[i++];
j++;
}
}
if (i<n1){
while (i<n1)
result[k++] = arr1[i++];
}
elseif(j<n2){
while (j<n2)
result[k++] = arr2[j++];
}
}
void main(){
int arr1[10], arr2[10], result[20], i;
int clrscr();
cout << "Enter 10 numbers [arr1]: ";
for (i = 0; i<10; i++)
cin >> arr1[i];
cout << "\nEnter 10 numbers [arr2]: ";
for (i = 0; i<10; i++)
cin >> arr2[i];

int item = mrg_sort(arr1, 10, arr2, 10, result);
cout << "\n\nAfter sort : ";
for (i = 0; i<item; i++)
cout << result[i] << " ";



_getch();

}
Don't spam people's inboxes.

You haven't made any changes to your code. Try fixing the things we recommended to you before posting back.
I made the changes. I am new to C++ please can you specifically point out this errors
Line 16,27: elseif is not valid else and if are separate keywords.

My compiler reports:
error C3861: 'elseif': identifier not found
That's pretty clear there is something wrong with elseif.

Line 31: Still missing a return statement.

Line 32: main must always be type int.

You have been asked to use code tags. PLEASE DO SO.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
I will not respond further until you apply code tags.

Topic archived. No new replies allowed.