If I place my arrays outside of the main function as global variables, my Merge-Sort works. If i declare unsortedArray or tempArray or BOTH inside of the main function, the terminal lights up with undeclared variable errors.
From Javascript I thought that nested functions can access stuff declared at a higher level. Since the Main() function is the highest level declaring all these others, shouldn't the sort() and merge() have access to anything declared in Main?
Am I just passing the parameters in incorrectly with the wrong name?
I guess just for convenience I had to declare them globally so I can call them freely when needed.
Since I pass unsortedArray at sort(unsortedArray, Start, End);
Does this mean each time my subroutines call (Array[]) they are calling in unsortedArray[]? How do I get the second one in there, if I wanted to declare it locally?
// Arrays MUST be declared outside of Main to work here??
int tempArray[8] = {};
int unsortedArray[8] = {4,2,6,8,1,3,7,5};
void sort(int Array[], int Start, int End);
void merge(int Array[], int Start, int Middle, int MiddlePlus, int End);
int main(void)
{
int Start = 0;
int End = 7;
sort(unsortedArray, Start, End);
}
void merge(int Array[], int Start, int Middle, int MiddlePlus, int End)
{
Does stuff with tempArray
Does stuff with unsortedArray
}
void sort(int Array[], int Start, int End)
{
if(End > Start)
{
int Middle = ((Start + End) / 2);
int MiddlePlus = Middle + 1;
sort(Array, Start, Middle);
sort(Array, Middle + 1, End);
merge(Array, Start, Middle, MiddlePlus, End);
}
How do I declare those Arrays locally and properly?