-2

Trying to make this code dynamic, but I am having errors. I have a basic merge sort algorithm that I want to make dynamic (so the user can input his own numbers) and I am sure it would be like a 5min work but I am unable to do it.

#include <iostream>

using namespace std;
//merginimas



void merge(int arr[], int l, int m, int r)
{
    int i = l; //kaires
    int j = m + 1; //desines
    int k = l; //laikinasis (i kuri sueina)

    int size = (r - 1) + 1;
    int temp[size]; // laikinas
    
    while (i<= m && j<=r)
    {
        if (arr[i] <= arr[j]) //lygina kaires ir desines array skaicius
        {
            temp[k] = arr[i];  // arr[i] mazesnis uz arr[j]
            i++;
            k++;
        }
        else
        {
            temp[k] = arr[j];  // arr[j] mazesnis uz arr[i]
            j++;
            k++;
        }
    }
    while (i<=m)
    {
        temp[k] = arr[i]; //kopijuoja skaicius is kaires i laikina
        i++;
        k++;
    }
    while (j <= r)
    {
        temp[k] = arr[j]; //kopijuoja skaicius is desines i laikina
        i++;
        k++;
    }
    for (int x = l; x <= r; x++)
    {
        arr[x] = temp[x];
    }
}

//sortinimas

void mergeSort(int arr[], int l, int r)
{
    if (l < r)
    {
        int m = (l + r) / 2;
        mergeSort(arr, 0, m);
        mergeSort(arr, m+1, r);
        merge(arr, l, m, r);
    }
}



int main()
{
    int size;
    int* ptr;

    cout << "Kiek skaiciu noresite surusiuoti? " << endl;//enter size of the array
    cin >> size;

    ptr = new int[size];
    

    cout << "Iveskite " << size << " skaicius kuriuos noresite surusiuoti: " << endl;//enter the numbers that you want to sort
    int myarr[size];
    for (int i = 0; i < size; i++)
    {
        cin >> myarr[i];
    }
    //int myarr[size];

    

    cout << "Before merge sort: " << endl;
    for (int i = 0; i < size; i++)
    {
        cout << myarr[i] << " ";
    }

    // mergesort function

    mergeSort(myarr, 0, (size - 1));

    cout << "After merge sort: " << endl;
    for (int i = 0; i < size; i++)
    {
        cout << myarr[i] << " ";
    }


    return 0;
}
James Z
  • 12,104
  • 10
  • 27
  • 43
  • 1
    I suggest that you use a [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) to store the data. A `vector` can grow dynamically. Also, there is already a [`std::merge`](https://en.cppreference.com/w/cpp/algorithm/merge) function template that merges sorted ranges. I suggest that you use that one. – Ted Lyngmo May 22 '22 at 16:55
  • what eror? ", i am having errors." – pm100 May 22 '22 at 16:56
  • 1
    i want to make dynamic (so the user can input his own numbers) – saim kazmi May 22 '22 at 16:56
  • kindly fix the errors. i have less time for this work – saim kazmi May 22 '22 at 16:57
  • What about her own numbers? – user4581301 May 22 '22 at 16:57
  • [C++ doesn't have variable-length arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Use `std::vector` for all your "dynamic array" needs. – Some programmer dude May 22 '22 at 16:57
  • 1
    variable length arrays are non standard in c++ `int temp[size]` etc. use std::vector instead. – pm100 May 22 '22 at 16:58
  • ***kindly fix the errors*** Except for `int temp[size]` which is illegal in standard c++ we don't know what errors you have. – drescherjm May 22 '22 at 16:58
  • Usage note: Always include the error messages you receive in plain text. It helps direct answerers to the specific problem you are dealing with. – user4581301 May 22 '22 at 16:59
  • 2
    Please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then **[edit]** your question to improve it, like copy-pasting any possible build errors (as text) into your question, and adding comments on the lines where you get the errors. – Some programmer dude May 22 '22 at 16:59
  • How is it not dynamic? `cin >> myarr[i]` lets the user input his own numbers. – David G May 22 '22 at 17:12

1 Answers1

1

this loops forever

while (j <= r)
{
    temp[k] = arr[j]; //kopijuoja skaicius is desines i laikina
    i++;
    k++;
}

and runs k to values outside the bounds of temp (once I changed temp to not be a variable lenghth array)

You never change j or r in the loop, so it just keeps going

pm100
  • 42,706
  • 22
  • 76
  • 135