Question

This is the code that I have, and its intention is to take a an array, load it with values 1-100, re-arrange them with a random number, and re-sort them. The randomizeArray function will not compile and I do not understand why. Is it not declared properly? If so, how do I fix it.

I got the error message:

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

on the line declaring 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*/
Was it helpful?

Solution

From the error message in your comment:

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

You are using a compiler that doesn't support C99, or you are not setting it in C99 mode. In C89, variables must be declared in the beginning of a block, so in this piece of code:

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

The variable currPos must be declared before calling srand:

void randomizeArray (int mainArr [], int arraysize)
{
   int seed = 10;/*Seed for the randon number operation*/
   int currPos = 0;
   srand(seed);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top