我試圖在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;
}
}
}
你用C或C++編程嗎?在第一種情況下,確保你的編譯器知道VLA(C99和可能在上面)。在後面的例子中,使用STL的數據類型。 – Matthias
只有新的編譯器可以聲明非常量大小的數組。 Borland不適合這個。你應該學習malloc或者使用一些不變的最大尺寸。 –
@Matthias Borland C++太老了,不知道C99 –