【问题标题】:The function with srand will not compile带 srand 的函数不会编译
【发布时间】: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


【解决方案1】:

来自您评论中的错误消息:

(62): error C2143: syntax error : missing ';' before 'type' ` 

您使用的编译器不支持 C99,或者您没有将其设置为 C99 模式。在 C89 中,变量必须在块的开头声明,所以在这段代码中:

void randomizeArray (int mainArr [], int arraysize)
{
   int seed = 10;/*Seed for the randon number operation*/
   srand(seed);
   int currPos = 0;

变量currPos必须在调用srand之前声明:

void randomizeArray (int mainArr [], int arraysize)
{
   int seed = 10;/*Seed for the randon number operation*/
   int currPos = 0;
   srand(seed);

【讨论】:

    猜你喜欢
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 2023-03-22
    • 2012-12-26
    相关资源
    最近更新 更多