【发布时间】:2014-02-23 05:43:15
【问题描述】:
这是我的代码,它的目的是获取一个数组,用值 1-100 加载它,用随机数重新排列它们,然后重新排序。 randomizeArray 函数无法编译,我不明白为什么。它没有正确声明吗?如果是这样,我该如何解决。
我收到错误消息:
(62):错误 C2143:语法错误:缺少 ';'在“类型”之前
在声明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):错误 C2143:语法错误:缺少 ';'在“类型”之前
-
在声明currPos的那一行
标签: c compiler-errors srand