我试图在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 –