This code runs on 4 different sites, so far I haven't tested more than that, but does not work on the Visual Studio program. The error is with n1 and n2. The errors are that "expression did not evaluate to a constant" and "array type 'int[n1]' is not assignable". I don't understand why the error occurs on the Visual Studio program but no the online compilers. I read something about increasing level of checking for errors on the online compilers but the 4 I used I did not see an option to do so. How would I resolve this error?
#include <iostream>
#include <stdio.h>
#include <list>
#include <chrono>
#include <algorithm>
using namespace std::chrono;
using namespace std;
// Part 2 to using Merge sort to sort my list
void merge(int arr[], int z, int l, int arr_size)
{
int n1 = l - z + 1;
int n2 = arr_size - l;
int L[n1], R[n2]; //expression error occurs here for both n1 and n2
for (int i = 0; i < n1; i++)
L[i] = arr[z + i]; // array type error occurs here
for (int j = 0; j < n2; j++)
R[j] = arr[l + 1 + j]; // array type error occurs here
int i = 0;
int j = 0;
int k = z;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
//Function that uses Merge sort to sort my list
void mergeSort(int arr[], int z, int arr_size)
{
int l;
if(z < arr_size)
{
l = (z + arr_size)/ 2;
mergeSort(arr, z, l);
mergeSort(arr, z+1, arr_size);
merge(arr, z, l, arr_size); //< not declared in scope?
}
}
//Function to print out results of sorting using Merge sort method
void mergePrint(int arr[], int arr_size)
{
int i;
cout << "Using Merge Sort method: ";
for (i = 0; i < arr_size; i++)
cout << arr[i] << " ";
}
//Part 3 to using Quick sort to sort my list
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
//Part 2 to using Quick sort to sort my list
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element and indicates the right position of pivot found so far
for (int j = low; j <= high - 1; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
//Function that uses Quick sort to sort my list
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
//Function to print out results of sorting using Quick sort method
void quickPrint(int arr[], int arr_size)
{
int i;
cout << "Using Quick Sort method: ";
for (i = 0; i < arr_size; i++)
cout << arr[i] << " ";
}
/*
//Function that uses Median sort to sort my list
void medianSort()
{
}
//Function to print out results of sorting using Median sort method
void medianPrint()
{
int i;
cout << "Using Median Sort method: ";
for (i = 0; i < arr_size; i++)
cout << arr[i] << " ";
}
*/
//Function that uses Insertion sort to sort my list
void insertionSort(int arr[], int arr_size)
{
int i,j,k;
for (i = 1; i < arr_size; i++)
{
k = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > k)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = k;
}
}
//Function to print out results of sorting using Insertion sort method
void insertionPrint(int arr[], int arr_size)
{
int i;
cout << "Using Insertion Sort method: ";
for (i = 0; i < arr_size; i++)
cout << arr[i] << " ";
}
int main()
{
// Declaring numbers
int arr[] = {6, 10, 12, 3, 7, 1}; // tried with 8 values but Merge sort does not sort entirely
int arr_size = sizeof(arr)/ sizeof(arr[0]);
//Captures time it takes to run Merge Sort function
auto mStart = high_resolution_clock::now();
//Call Merge sort functions
mergeSort(arr, 0, arr_size - 1);
mergePrint(arr, arr_size);
auto mStop = high_resolution_clock::now();
auto mDuration = duration_cast<microseconds>(mStop - mStart);
cout << "\nTime taken by Merge Sort function : "<< mDuration.count() << " microseconds" <<endl;
//Captures time it takes to run Quick Sort function
auto qStart = high_resolution_clock::now();
//Call Quick sort functions
quickSort(arr, 0, arr_size - 1);
quickPrint(arr, arr_size);
auto qStop = high_resolution_clock::now();
auto qDuration = duration_cast<microseconds>(qStop - qStart);
cout << "\nTime taken by Quick Sort function : "<< qDuration.count() << " microseconds" <<endl;
//Captures time it takes to run Insertion Sort function
auto iStart = high_resolution_clock::now();
//Calls Insertion Sort functions
insertionSort(arr, arr_size);
insertionPrint(arr, arr_size);
auto iStop = high_resolution_clock::now();
auto iDuration = duration_cast<microseconds>(iStop - iStart);
cout << "\nTime taken by Insertion Sort function : "<< iDuration.count() << " microseconds";
return 0;
}