简体   繁体   中英

Declaring Array of variable size in C

I have been going through Introduction to Algorithms and am trying to implement the pseudo-code presented for merge-sort using C programming language.

This is the pseudo-code presented for the MERGE procedure:

在此处输入图片说明

While I understand this procedure, I am struggling to implement in C when arriving at the 3rd line. My compiler is giving an error (and rightly so after C99) expression must have a constant value .

Error is occurring on line 3 of the pseudo-code or at int L[n1]; in code posted below

How can I create an array which holds n1 and n2 values which are constantly changing from one iteration to the next? Any suggestions would be appreciated.

I also came across https://www.geeksforgeeks.org/merge-sort/ to see how it might be done, and the website is using the same syntax as I am, but without any compiler warnings. Is this because of an older compiler version (C99?) or am I missing something from my end?

My code is below:

/* C program for Merge Sort */
#include<stdlib.h>
#include<stdio.h>

#define infinite 9999;      //Used for sentinels

void MERGE(A, p, q, r);
void printArray(Arr, size);
void MERGE_SORT(A, p, r);

int main(void)
{
   int A[] = { 12, 11, 13, 5, 6, 7, 2, 9 };
   int arr_size = sizeof(A) / sizeof(A[0]);

   MERGE_SORT(A, 1, arr_size);

   printf("\nSorted array is \n");
   printArray(A, arr_size);

   return 0;
 }

 void MERGE(int A[], int p, int q, int r)
 {
   int i = 0;
   int j =0;
   int n1 = q - p + 1;      //Computing length of sub-array 1
   int n2 = r - q;          //Computing length of sub-array 2
   int L[n1];               //Creating Left array
   int R[n2];               //Creating Right array

   for (int i = 1; i < n1; i++) {
       L[i] = A[p + i - 1];
   }
   for (int j = 1; j < n2; j++) {
       L[j] = A[q + j];
   }

   L[n1] = 99;  //Placing Ssentinel at the end of array
   R[n2] = 99;

   i = 1;
   j = 1;

   /*Prior to the first iteration k = p, so the subarray is empty.
   Both L[i] and R[j] are the smallest elements of their arrays and have not 
   been copied back to A*/
   for (int k = p; k < r; k++) {
       if (L[i] <= R[j]) {
           A[k] = L[i];
           i++;
       }
       else if (A[k] = L[i])
           j++;
   }

}

void MERGE_SORT(int A[], int p, int r)
{
   //During first iteration p = 1 & r = 8
   if (p < r) {
       int q = (p + r) / 2;
       MERGE_SORT(A, p, q);
       MERGE_SORT(A, q + 1, r);
       MERGE(A, p, q, r);
   }
}

EDIT

Code for MERGE updated as follows thanks to suggestions from answer and comments below. Even though the code below presents no syntax or run-time errors, the output is still not correct. This is out of the scope of the question, however. Another question was asked here: Writing Merge Sort Pseudo-Code Procedure in C

 void MERGE(int A[], int p, int q, int r)
 {
   int i = 0;
   int j =0;
   int n1 = q - p + 1;                      
   int n2 = r - q;                          
   int *L = malloc((n1+1) * sizeof(*L));        //Creating Left array
   int *R = malloc((n2+1) * sizeof(*R));            //Creating Right array

   for (int i = 1; i < n1; i++) {
       L[i] = A[p + i - 1];
   }
   for (int j = 1; j < n2; j++) {
       L[j] = A[q + j];
   }

   L[n1] = 99;  //<-- Some modification must be carried out here to allocate 
   R[n2] = 99;  //`99` to the end of array

   i = 1;
   j = 1;

   for (int k = p; k < r; k++) {
       if (L[i] <= R[j]) {
           A[k] = L[i];
           i++;
       }
       else if (A[k] == L[i])
           j++;
   }

   free(L);
   free(R);  //Freeing both pointers at the end of iteration
}

To create an array with size computed at runtime, use malloc()

int *L = malloc(n1 * sizeof(*L));
if (L == NULL) {
    // handle error
}

The code you linked to is using Variable Length Arrays which is supported by some but not all C compilers. See :

Passing array to a function (and why it does not work in C++)

This is accessing past the end of the array

L[n1] = 99;  //<-- Some modification must be carried out here to allocate 
R[n2] = 99;  //`99` to the end of array

Remember, with an array containing n elements, valid indices are 0 - n-1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM