这是我的代码,它的目的是获取一个数组,用值 1-100 加载它,用随机数重新排列它们,然后重新排序。 randomizeArray
函数无法编译,我不明白为什么。它没有正确声明吗?如果是这样,我该如何解决。
我收到错误信息:
(62): error C2143: syntax error : missing ';' before 'type'
在声明 currPos
的行上。
#include <stdio.h>
#include <stdlib.h>
/*Set prototypes for functions used*/
void bubblesortArray(int[],int);
void printArray(int[], int);
void randomizeArray (int[], int);
int main()
{
const int arraysize = 100;/*Defines the size of the array*/
int mainArr [100];/*Declares the array, with the size above*/
int index = 0;
for(index; index<arraysize; index++)
{
mainArr[index] = index + 1;
}
printArray(mainArr, arraysize);
randomizeArray (mainArr, arraysize);
printArray(mainArr, arraysize);
bubblesortArray(mainArr, arraysize);
printArray(mainArr, arraysize);
getchar();
}
void printArray(int mainArr[], int arraysize)
{
int printindex = 0;/*INdex of the printing Array*/
for(printindex; printindex<arraysize; printindex++)/*Prints the values of the Array on the screen*/
{
printf ("%5d,", mainArr[printindex]);
if(((printindex+1)%10) == 0)
printf("\n");
}
}/*End of print function*/
void randomizeArray (int mainArr [], int arraysize)
{
int seed = 10;/*Seed for the randon number operation*/
srand(seed);
int currPos = 0;
for(currPos; currPos<arraysize; currPos++)
{
int swapval = rand()% 99;/*Sets a random pointer value for swapping in the array*/
int temp = mainArr[currPos];
mainArr[currPos] = mainArr[swapval];
mainArr[swapval] = temp;
}
}
void bubblesortArray(int mainArr[], int arraysize)
{
int sortloop1 = 0;/*The first index fo the sort algorithm*/
int sortloop2 = 0;/*The second(inner) index fo the sort algorithm*/
for(sortloop1;sortloop1<arraysize;sortloop1++) /*Sort algorithm to get the values in their correct places.*/
{
for(sortloop2=sortloop1;sortloop2<(arraysize);sortloop2++)
{
if(mainArr[sortloop1]>mainArr[sortloop2])
{
int temp=mainArr[sortloop1];
mainArr[sortloop1]=mainArr[sortloop2];
mainArr[sortloop2]=temp;
} /*End of sort operation*/
} /*End of inner sorting loop*/
} /* End of sort algorithm*/
}/*End of bubble sort function*/
最佳答案
来自您评论中的错误消息:
(62): error C2143: syntax error : missing ';' before 'type' `
您正在使用不支持 C99 的编译器,或者您没有将其设置为 C99 模式。在C89中,变量必须声明在 block 的开头,所以在这段代码中:
void randomizeArray (int mainArr [], int arraysize)
{
int seed = 10;/*Seed for the randon number operation*/
srand(seed);
int currPos = 0;
必须在调用srand
之前声明变量currPos
:
void randomizeArray (int mainArr [], int arraysize)
{
int seed = 10;/*Seed for the randon number operation*/
int currPos = 0;
srand(seed);
关于c - 带有 srand 的函数将无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21964689/