I tried to run this c program in borland c++. It shows "expression required", while declaring the array int holes[size]
. This is the only error that is shown. I tried to solve it but it still shows the same problem.
我试着在borland c ++中运行这个c程序。它显示“需要表达式”,同时声明数组int holes [size]。这是唯一显示的错误。我试图解决它,但它仍然显示同样的问题。
How can I solve this problem?
我怎么解决这个问题?
/*
* 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;
}
}
}
2 个解决方案
#1
6
int holes[size]
is a variable-length array (VLA), a feature that has "only" been around in C for 17 years and is not supported by C++.
int holes [size]是一个可变长度数组(VLA),这个特性在C语言中“仅”存在了17年,并且不受C ++支持。
So it would seem that you either have a completely outdated compiler (Borland has not released any compilers for the past 10 years) or you are trying to compile C code with a C++ compiler. Neither will work.
所以看起来你要么有一个完全过时的编译器(Borland在过去的10年里没有发布过任何编译器),要么你正在尝试用C ++编译器编译C代码。两者都不会起作用。
If by "Borland" you happen to mean Embarcadero C++ Builder, then you merely have to tell it to compile the code as C instead of C++.
如果用“Borland”你碰巧意味着Embarcadero C ++ Builder,那么你只需告诉它将代码编译为C而不是C ++。
Otherwise, you will have to upgrade to a modern C compiler, like for example GCC/Mingw. For example by downloading the completely free Windows version of the Codeblocks IDE, which comes with that compiler pre-installed.
否则,您将不得不升级到现代C编译器,例如GCC / Mingw。例如,通过下载完全免费的Windows版本的Codeblocks IDE,该版本随预装的编译器一起提供。
#2
-1
in third parameter of function pigeonhole_sort you declare a pointer and in call statement you are passing an array
在函数pigeonhole_sort的第三个参数中,您声明一个指针,并在调用语句中传递一个数组
#1
6
int holes[size]
is a variable-length array (VLA), a feature that has "only" been around in C for 17 years and is not supported by C++.
int holes [size]是一个可变长度数组(VLA),这个特性在C语言中“仅”存在了17年,并且不受C ++支持。
So it would seem that you either have a completely outdated compiler (Borland has not released any compilers for the past 10 years) or you are trying to compile C code with a C++ compiler. Neither will work.
所以看起来你要么有一个完全过时的编译器(Borland在过去的10年里没有发布过任何编译器),要么你正在尝试用C ++编译器编译C代码。两者都不会起作用。
If by "Borland" you happen to mean Embarcadero C++ Builder, then you merely have to tell it to compile the code as C instead of C++.
如果用“Borland”你碰巧意味着Embarcadero C ++ Builder,那么你只需告诉它将代码编译为C而不是C ++。
Otherwise, you will have to upgrade to a modern C compiler, like for example GCC/Mingw. For example by downloading the completely free Windows version of the Codeblocks IDE, which comes with that compiler pre-installed.
否则,您将不得不升级到现代C编译器,例如GCC / Mingw。例如,通过下载完全免费的Windows版本的Codeblocks IDE,该版本随预装的编译器一起提供。
#2
-1
in third parameter of function pigeonhole_sort you declare a pointer and in call statement you are passing an array
在函数pigeonhole_sort的第三个参数中,您声明一个指针,并在调用语句中传递一个数组