Hello, I need some help filling an array with UNIQUE random numbers. So far I've figured out how to fill an array with random numbers, that's easy, but I'm stuck on how to avoid filling it with duplicate values. I'm assuming that i'll have to use either a linear or binary search function while filling the array, but my attempts so far have failed. Anyone got any idea what i'm doing wrong? Here's my terrible code so far...any help would dearly be appreciated.
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
//Global constant declaration
const int MAXSIZE = 200;
const int SEED = 12;
//Function Prototypes
void Seed();
void FillMainList(int list[]);
void PrintArray(const int list[]);
int LinearSearch(const int list[], int value);
int main ()
{
int list[MAXSIZE];
Seed();
FillMainList(list);
PrintArray(list);
return 0;
}
void Seed() //Set & Display the seed value
{
srand(SEED);
}
void FillMainList(int list[])
{
int temp, test;
for (int i=0; i<MAXSIZE; i++)
{
temp = (rand() % 5000);
test = LinearSearch(list, temp);
if (test == -1)
list[i] = temp;
}
}
void PrintArray(const int list[])
{
for (int i=0; i<MAXSIZE; i++)
{
cout << setw(5) << list[i] << endl;
}
cout << endl;
}
int LinearSearch(const int list[], int value)
{
int index = 0;
int position = -1;
bool found = false;
while (index < MAXSIZE && !found)
{
if (list[index] == value)
{
found = true;
position = index;
}
index++;
}
return position;
}