'Make basic merge sort algorithm dynamic so the user can input his own numbers [closed]

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;
}


Solution 1:[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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pm100