I've been working on this code which generates an array of random numbers into numArray and then it shuffles it, adds it to the array and prints out the array. This is the code I've completed.
#include <stdio.h>
#include <stdlib.h>
#define SEED 7451
#define ARRAY_SIZE 25
#define LOOPS 10
void InitializeArray(int *numArray, const int arrayLength);
void ShuffleArray(int *numArray, const int arrayLength);
void printArray(int *numArray, const int arrayLength);
int main()
{
//informational message
printf("This Program creates permutations of an array of numbers/n");
//declare numArray with ARRAY_SIZE values as int
int numArray[ARRAY_SIZE];
//seed the random number generator
srandom(SEED);
int count;
int arrayLength= sizeof(numArray); //sizeof(numArray)/sizeof(numArray[0]);
//LOOPED 10 times, use the LOOPS variable
for (count = 0; count < LOOPS; count++){
//call InitializeArray
InitializeArray(numArray,arrayLength);
//call printArray Function to print out normal Array
printf("Initial Array: \n");
printArray(numArray,arrayLength);
//call ShuffleArray to create permutation
ShuffleArray(numArray,arrayLength);
//call printArray Function to print out the shuffled list.
printf("Shuffled Array: \n");
printArray(numArray,arrayLength);
}
return 0;
}
void InitializeArray(int *numArray, const int arrayLength)
{
//adds the values in ARRAY_SIZE to the numArray
int i;
for (i=0; i< ARRAY_SIZE;i++)
numArray=random()%(ARRAY_SIZE+1);
}
void ShuffleArray(int *numArray, const int arrayLength)
{
int i,j,temp;
for(j = 1; j < arrayLength; j++){
temp=numArray[j];
i=j-1;
while((i >= 0) && (numArray[i] > temp)){
numArray[i+1]=numArray[i];
i--;
}
numArray[i+1]=temp;
}
}
void printArray(int *numArray, const int arrayLength)
{
int i; // initialize counter
for(i = 0; i < arrayLength; i++)
{
printf("%d \n",numArray[i]);
}
}
For some reason my random number generator keeps giving me errors.
numArray=random()%(ARRAY_SIZE+1);
Gives me an error. It says there is casting error which I don't understand. Can someone help me fix the random number generator.
Aucun commentaire:
Enregistrer un commentaire