Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
#include <iostream>
using namespace std;
void mr(int arr[], int p, int q, int r) {
  int n1 = q - p + 1;
  int n2 = r - q;
  int lower[n1], higher[n2];
  for (int i = 0; i < n1; i++){
    lower[i] = arr[p + i];}
  for (int j = 0; j < n2; j++){
    higher[j] = arr[q + 1 + j];}
  int i, j, k;
  i = 0;
  j = 0;
  k = p;
  while (i < n1 && j < n2) {
    if (lower[i] <= higher[j]) {
      arr[k] = lower[i];
      i++;
    } else {
      arr[k] = higher[j];
      j++;
    }
    k++;
  }
  while (i < n1) {
    arr[k] = lower[i];
    i++;
    k++;
  }

  while (j < n2) {
    arr[k] = higher[j];
    j++;
    k++;
  }
}
void ms(int arr[], int low, int high) {
  if (low < high) {

    int m = low+(high-low)/2;
    cout << endl;
    cout << "[";
    for(int a = low; a <= m; a++){
        cout << arr[a] << " ";
    }
    cout << "]";
    cout << "|";
    cout << "[";
    for(int b = m + 1; b <= high; b++){
            cout << arr[b] << " ";
    }
    cout << "]";

    ms(arr, low, m);
    ms(arr, m + 1, high);
    mr(arr, low, m, high);
    cout << endl;
    cout << "[";
    for(int c = low; c <= high; c++){
        cout << arr[c] << " ";
    }
    cout << "]";
  }
}
int main()
{
    system("Color 07");
    char yn,ord;

        int numel, arr[11],sz;
        cout<<"Lance Grayson D. Musngi\nBSIT 2 - C\n\n"<<endl;
        cout<<"\t\t\t==================================="<<endl;
        cout<<"\t\t\t|::::::::::MERGE SORTING::::::::::|"<<endl;
        cout<<"\t\t\t===================================\n\n\n"<<endl;
        cout<<"Enter number of elements you wish to be sorted (Min.5 - Max.12): ";
        cin>>numel;
        if(numel>=5&&numel<=12){
            system("cls");
            cout<<"Enter "<<numel<<" elements to be sorted:\n";
            sz = numel;
            for (int i = 0; i < numel; i++){
                    cout<<"Element ["<<i+1<<"]: ";
                cin>>arr[i];
            }
            cout<<"\n------------------------------------------------------"<<endl;
         ms(arr, 0, numel-1);
         cout<<"\n------------------------------------------------------"<<endl;
         cout<<"[A]scending Merge order\n[D]escending Merge order"<<endl;
         cin>>ord;
         if (ord=='a'||ord=='A'){
            cout << "[A]scending Merge Order Array: \n";
            for (int i = 0; i < sz; i++){
            cout << arr[i] << "\t";
            }
         }
         else if(ord=='d'||ord=='D'){
         cout << "[D]escending Merge Order Array: \n";
         for (int i = sz-1; i > -1; i--){
            cout << arr[i] << "|\t";
            }


What I have tried:

i tried using converter but its now working
Posted
Updated 29-Nov-22 10:48am
Comments
CPallini 28-Nov-22 3:45am    
Then try to convert it yourself. Then you could ask specific questions here.

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it'll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

In this case, it looks like it's already been converted from something to C with C++ headers tacked on because that is pretty poor quality code to start with!
 
Share this answer
 
Since the description of the program is missing and no description of errors is given, the help here is unnecessarily complicated. The function is obviously a merge sort algorithm.

As an error it is immediately noticeable that various arrays in C++ do not work like this. In the first place code is used that tries to declare an array with a variable. This might work with C, but not with C++. Here e.g. new() could help.
C++
// int lower[n1], higher[n2];
int *lower  = new int[n1];
int *higher = new int[n2];

Further down, a field is declared with a constant that could obviously become too large.
C++
int numel, arr[11], sz;
if (numel >= 5 && numel <= 12) {
   for (int i = 0; i < numel; i++)
      cin >> arr[i];	
}

Apparently the program is not complete, because at the end some things seem to be missing. The sorting function seems to work at first sight.

Note: Converting a very common sorting algorithm into another language does not seem to make sense except for practice. On the German Wikipedia page you can find e.g. also a version in Java. An iterative implementation using chained lists would also be much more readable in C++.
Mergesort – Wikipedia[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900