It gives Array type int m2 not assignable in Merge sort sorting function file. And also it gives expression must contain a constant value error.How can i fix that errors?

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

FOR C++

In this assignment, we are going to implement a couple of sorting algorithms and test their performances. So the idea is as follows:

You should write a program to sort an array. For this purpose, you will implement two sorting algorithms: Insertion Sort and Merge Sort. For each algorithm, you are supposed to have a counter and count how many times a comparison is made to sort the array. Once this logic is implemented, create integer arrays of 1000 and 10000 elements with random values inside (you can implement a random number generator for this purpose and randomize their elements).

Then go ahead and run your application. The output should show us how many comparisons it made for each algorithm for 1000 and 10000 elements.

 

 

//header file
#include<iostream>
#include<time.h>
//using namespace
using namespace std;

// Insertion sort sorting function it will take array and its size an argument
void insertionSort(int nums[], int m, int& comparisonsCount)
{
    int j, val, i;
    for (j = 1; j < m; j++)
    {
        val = nums[j];
        i = j - 1;
        comparisonsCount++; 
        while (i >= 0 && nums[i] > val)
        {
            comparisonsCount++; 
            nums[i + 1] = nums[i];
            i = i - 1;
        }
        nums[i + 1] = val;
    }
}

// Merge sort sorting function 

void merge(int nums[], int left, int middle, int right,int& comparisonsCount)
{
    int m1 = middle - left + 1;
    int m2 = right - middle;
    int L[m1], R[m2];

    for (int j = 0; j < m1; j++)
        L[j] = nums[left + j];
    for (int i = 0; i < m2; i++)
        R[i] = nums[middle + 1 + i];
    int j = 0;
    int i = 0;
 
    int s = left;
 
    while (j < m1 && i < m2) {
        if (L[j] <= R[i]) {
            comparisonsCount++;
            nums[s] = L[j];
            j++;
        }
        else {
            nums[s] = R[i];
            i++;
        }
        s++;
    }
    while (j < m1) {

        nums[s] = L[j];
        j++;
        s++;
    }
    while (i < m2) {
        nums[s] = R[i];
        i++;
        s++;
    }
}
void mergeSort(int nums[],int left,int right, int& comparisonsCount){
    if(left>=right){
        return;//returns recursively
    }
    int middle =left+ (right-left)/2;
    mergeSort(nums,left,middle,comparisonsCount);
    mergeSort(nums,middle+1,right,comparisonsCount);
    merge(nums,left,middle,right,comparisonsCount);
}
//main function
int main()
{
    srand(time(0));
    int arr_1[1000]; int copy_arr1[1000];
    int arr_2[10000]; int copy_arr2[10000];

    for(int j=0; j<1000; j++)
    {
        arr_1[j]=rand();
        copy_arr1[j]=arr_1[j];
    }
    int count_insertion=0;
    int count_merge=0;
    insertionSort(arr_1,1000,count_insertion);
    mergeSort(copy_arr1,0,1000-1,count_merge);

    cout<<"For 1000 length array: \n";
    cout<<"Comparision made for insertion sort: "<<count_insertion;
    cout<<"\nComparision made of merge sort : "<<count_merge;

 

    for(int j=0; j<10000; j++)
    {
        arr_2[j]=rand();
        copy_arr2[j]=arr_1[j];
    }
    count_insertion=0;
    count_merge=0;
    insertionSort(arr_2,10000,count_insertion);
    mergeSort(copy_arr2,0,10000-1,count_merge);

    cout<<"\n\nFor 10000 length array: \n";
    cout<<"Comparision made for insertion sort: "<<count_insertion;
    cout<<"\nComparision made of merge sort : "<<count_merge;
}

 

It gives Array type int m2 not assignable in Merge sort sorting function file.
And also it gives expression must contain a constant value error.How can i fix that errors?

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY