Click here to Skip to main content
13,252,065 members (56,290 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
The c ++ program for the merge when executed shows me in the output 8 errors I think they are all of the size of the array because it must be const. When executing, the array divides by half, which means it changes the size of the Array. I do not know if that is the reason, but I want you to help me as soon as possible. Thank

What I have tried:

#include<iostream>
using namespace std;
void merge(int a[], int low, int high, int mid)
{
cout << "Entered merge" << endl;
int n1 = high - low + 1;
int n2 = mid - high;
int L[n1 + 1] = 999;
int R[n2 + 1] = 999;
int i = 1, j = 1;
for (int i = 1; i <= n1; i++)
{
L[i] = a[low + i - 1];
}
for (int j = 1; j <= n2; j++)
{
R[j] = a[high + j];
}

for (int k = low; k <= mid; k++)
{
if (L[i] <= R[j])
{
a[k] = L[i];
i = i + 1;
}
else
{
a[k] = R[j];
j = j + 1;
}
}

}
void mergesort(int a[], int low, int high)
{
if (low < high)
{
int mid;
mid = (low + high) / 2;
mergesort(a, low, high);
mergesort(a, mid + 1, high);
merge(a, low, high, mid);
}
}
void main()
{
int size = 7;
int a[7] = { 38, 43, 27, 3, 9, 82, 10 };
int low = 0;
int high = size - 1;
mergesort(a, low, high);
for (int i = 0; i < 7; i++)
{
cout << a[i] << "  ";
}
cout << endl;
}
Posted 12-Aug-17 12:19pm
Updated 12-Aug-17 15:38pm
Comments
ppolymorphe 12-Aug-17 20:35pm
   
"when executed shows me in the output 8 errors"
Which errors ?
Member 13358623 14-Aug-17 10:32am
   
8 errors :

Error 1 error C2057: expected constant expression c:\users\aaaaaa\desktop\data structures\merge\merge\merge\main.cpp 83 1 merge
Error 2 error C2466: cannot allocate an array of constant size 0 c:\users\aaaaaa\desktop\data structures\merge\merge\merge\main.cpp 83 1 merge
Error 3 error C2440: 'initializing' : cannot convert from 'int' to 'int []' c:\users\aaaaaa\desktop\data structures\merge\merge\merge\main.cpp 83 1 merge
Error 4 error C2057: expected constant expression c:\users\aaaaaa\desktop\data structures\merge\merge\merge\main.cpp 84 1 merge
Error 5 error C2466: cannot allocate an array of constant size 0 c:\users\aaaaaa\desktop\data structures\merge\merge\merge\main.cpp 84 1 merge
Error 6 error C2440: 'initializing' : cannot convert from 'int' to 'int []' c:\users\aaaaaa\desktop\data structures\merge\merge\merge\main.cpp 84 1 merge
7 IntelliSense: expression must have a constant value c:\Users\aaaaaa\Desktop\Data Structures\merge\merge\merge\main.cpp 83 9 merge
8 IntelliSense: expression must have a constant value c:\Users\aaaaaa\Desktop\Data Structures\merge\merge\merge\main.cpp 84 9 merge
ppolymorphe 14-Aug-17 10:34am
   
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Think about what you do here and what you should.
mergesort(a, low, high);
mergesort(a, mid + 1, high);
merge(a, low, high, mid);


There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.171114.1 | Last Updated 12 Aug 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100