I am trying to make the random generator fill my array with no duplicates. An diplay the in order but aslo first line to print array[0], then second line array[0] and array[1], so on and so on until it display all twenty on one line.
Pease help
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
void insert(int [], int); // function prototype
bool AlreadyThere(int array[], int value, int index); // called [],asize,call from init_call
int main()
{
int count;
const int MAXNUM = 20;
int id[MAXNUM];
int newcode;
bool lookingForNew;
srand(time(NULL));
for(count = 0; count<MAXNUM; count ++)
{
lookingForNew= true;
while(lookingForNew)
{
newcode = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 50.0;
//cout<<"x"<<newcode<<endl;
//if(AlreadyThere(id,newcode,count))
if (id[count] == count || count == 0)
{
id[count]=newcode;
lookingForNew = false;
cout<<id[0]<<id[1]<<endl;
}
}
}
insert(id,newcode);
return 0;
}
void insert(int idcode[], int newcode)
{
int i, newpos, trlpos;
// find correct position to insert the new code
i = 0;
while (idcode[i] < newcode)
i++;
newpos = i; // found the position for the new code
// find the end of the list
while (idcode[i] != 51)
i++;
trlpos = i;
// move idcodes over one position
for (i = trlpos; i >= newpos; i--)
idcode[i+1] = idcode[i];
// insert the new code
idcode[newpos] = newcode;
return;
}
Code tags added. It's code, not c, btw. -Narue