初始化数组

时间:2015-10-06 10:03:20

标签: c arrays random permutation random-seed

我一直在研究这个代码,它会在numArray中生成一个随机数组,然后将其混洗,将其添加到数组并打印出数组。这是我已经完成的代码。

#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]);
}
}

由于某种原因,我的随机数生成器不断给我错误。

numArray=random()%(ARRAY_SIZE+1);

给我一​​个错误。它说有铸造错误,我不明白。有人可以帮我修复随机数生成器。

2 个答案:

答案 0 :(得分:0)

首先函数random()不存在,我想你的意思是rand()。 srandom()也应该是srand()。

numArray=random()%(ARRAY_SIZE+1);必须为numArray[i]=rand();。如果要在某个范围内指定整数,可以使用%(即范围0-10:rand()%11)。

您的arrayLength变量尚未正确,因为sizeof(numArray)以字节为单位返回数组的大小。要获得elemts的数量,你必须除以类型的大小: int arrayLength= sizeof(numArray)/sizeof(int);

答案 1 :(得分:0)

不完全重新排序数组中的成员但是,它在您想要生成随机数的范围内的分布上做得不错。 如果它对你有所帮助,瞧:

// get random number within provided base + exponent
// by Goran Biljetina --> 2012

function isEmpty(value){
    return (typeof value === "undefined" || value === null);
}
var numSeq = new Array();
function add(num,seq){
    var toAdd = new Object();
     toAdd.num = num;
     toAdd.seq = seq;
     numSeq[numSeq.length] = toAdd;
}
function fillNumSeq (num,seq){
    var n;
    for(i=0;i<=seq;i++){
        n = Math.pow(num,i);
        add(n,i);
    }
}
function getRandNum(base,exp){
    if (isEmpty(base)){
        console.log("Specify value for base parameter");
    }
    if (isEmpty(exp)){
        console.log("Specify value for exponent parameter");
    }
    fillNumSeq(base,exp);
    var emax;
    var eseq;
    var nseed;
    var nspan;
    emax = (numSeq.length);
    eseq = Math.floor(Math.random()*emax)+1;
    nseed = numSeq[eseq].num;
    nspan = Math.floor((Math.random())*(Math.random()*nseed))+1;
    return Math.floor(Math.random()*nspan)+1;
}

console.log(getRandNum(10,20),numSeq);
//getRandNum(-10,20);
//console.log(getRandNum(-10,20),numSeq);
//console.log(numSeq);