2016-12-01 83 views
0

我試圖在borland C++中運行這個c程序。 它顯示「需要表達」,同時聲明數組int holes[size]。 這是顯示的唯一錯誤。我試圖解決它,但它仍然顯示相同的問題。我不能運行一個c程序

我該如何解決這個問題?

/* 
     * C Program to Implement Pigeonhole Sort 
     */ 
    #include <stdio.h> 

    #define MAX 7 

    void pigeonhole_sort(int, int, int *); 
    void main() 
    { 
     int a[MAX], i, min, max; 
     printf("enter the values into the matrix :"); 
     for (i = 0; i < MAX; i++) 
     { 
      scanf("%d", &a[i]); 
     } 
     min = a[0]; 
     max = a[0]; 
     for (i = 1; i < MAX; i++) 
     { 
      if (a[i] < min) 
      { 
       min = a[i]; 
      } 
      if (a[i] > max) 
      { 
       max = a[i]; 
      } 
     } 
     pigeonhole_sort(min, max, a); 
     printf("Sorted order is :\n"); 
     for (i = 0; i < MAX; i++) 
     { 
      printf("%d", a[i]); 
     } 
    } 

    /* sorts the array using pigeonhole algorithm */ 
    void pigeonhole_sort(int mi, int ma, int * a) 
    { 

     int size, count = 0, i; 
     int *current; 
     current = a; 
     size = ma - mi + 1; 
     int holes[size]; 
     for (i = 0; i < size; i++) 
     { 
      holes[i] = 0; 
     } 
     for (i = 0; i < size; i++, current++) 
     { 
      holes[*current-mi] += 1; 
     } 
     for (count = 0, current = &a[0]; count < size; count++) 
     { 
      while (holes[count]--> 0) 
      { 
       *current++ = count + mi; 
      } 
     }   
    } 
+1

你用C或C++編程嗎?在第一種情況下,確保你的編譯器知道VLA(C99和可能在上面)。在後面的例子中,使用STL的數據類型。 – Matthias

+1

只有新的編譯器可以聲明非常量大小的數組。 Borland不適合這個。你應該學習malloc或者使用一些不變的最大尺寸。 –

+2

@Matthias Borland C++太老了,不知道C99 –

回答

0
在功能pigeonhole_sort的第三個參數

聲明一個指針和在調用語句要傳遞的陣列

+0

這是一條評論,而不是答案。 –

+0

@Michael Walz,<50代表不能寫評論 – MrMuMu

+0

@NanonA你必須先獲得該特權。 –

6

int holes[size]是一個可變長度數組(VLA),即「僅」是圍繞特徵在C中使用了17年,並且不被C++支持。

因此,您似乎有一個完全過時的編譯器(Borland在過去的10年中沒有發佈任何編譯器),或者您試圖使用C++編譯器編譯C代碼。兩者都不會起作用。


如果「Borland的」你碰巧是指英巴卡迪諾C++ Builder中,那麼你只需要告訴它來編譯代碼爲C,而不是C++。

否則,您將不得不升級到現代C編譯器,例如GCC/Mingw。例如,下載完全免費的Windows版本的Codeblocks IDE,該版本預裝了該編譯器。

+0

我使用Borland版本5.02,代碼將如何?! – ssss

+1

@ssss這將是廢話。 BC 5.02不支持C99,它不支持C++ 98。沒有理由爲什麼你會在2016年使用BC 5.02。 – Lundin

+0

這是我們在學院使用的程序。我會嘗試其他程序來運行它。感謝您的幫助@Lundin – ssss