error in implementation of bucket sort
-
08-12-2019 - |
题
here is my code from which can't understand where is error
#include <iostream>
#include<iomanip>
using namespace std;
#define narray 8;// array size;
#define nbucket 5;// bucket size;
#define interval 10;// bucket range
struct node
{
int data;
struct node *next;
};
void BucketSort(int arr[]);
struct node *InsertionSort(struct Node *list);
void print(int arr[]);
void printBuckets(struct Node *list);
int getBucketIndex(int value);
void BucketSort(int arr[])
{
int i,j;
struct node **buckets;
buckets = (struct node **)malloc(sizeof(struct node*) * nbucket);
for (i=0;i<nbucket;i++){
buckets[i]=NULL;
}
for (int i=0;i<narray;i++){
struct node *current;
int pos=getBucketIndex(arr[i]);
current=(struct node *)malloc(sizeof(struct node));
current->data=arr[i];
current->next=buckets[pos];
buckets[pos]=current;
}
}
int main(){
return 0;
}
errors are a lot of,for example
Error 1 error C2143: syntax error : missing ')' before ';' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp 22 bucket_sort
Error 2 error C2059: syntax error : ')' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp 22 bucket_sort
Error 3 error C2146: syntax error : missing ')' before identifier 'i' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp 23 bucket_sort
Error 4 error C2059: syntax error : ';' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp 23 bucket_sort
Error 5 error C2059: syntax error : ')' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp 23 bucket_sort
Error 6 error C2143: syntax error : missing ';' before '{' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp 23 bucket_sort
Error 7 error C2146: syntax error : missing ')' before identifier 'i' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp 26 bucket_sort
Error 8 error C2059: syntax error : ';' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp 26 bucket_sort
Error 9 error C2059: syntax error : ')' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp 26 bucket_sort
Error 10 error C2143: syntax error : missing ';' before '{' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp 26 bucket_sort
解决方案
Remove the ;
from #define nbucket 5;
and the other defines.
At the moment, line 22 is expanded by the preprocessor to become the obviously-invalid:
buckets = (struct node **)malloc(sizeof(struct node*) * 5;);
// ^^
其他提示
The #define
lines are not part of the compiler, it's part of a pre processor that runs before the compiler. The lines handled by the pre-processor should not be terminated with semicolon.
Take for example this line:
#define narray 8;// array size;
This creates a macro named narray
. When the pre-processor runs it replaces all instances of narray
with the replacement text, in this case 8;
. As you can see, having 8;
inside an expression will add a semicolon where there shouldn't be any.
不隶属于 StackOverflow