I need help writing a lottery program that generates 5 non duplicate numbers that is between 1 and 20 by using arrays. Here is my code and i cant compile it.
What is in line 21? Remove it.
In lines 22-28 you check if random number already exist. So you should compare it with existed values. Rewrite "5" in line 22 as "count".
Will it work?
#include <iostream>
#include <stdlib.h>
#include <time.h>
int main()
{
// seed the random number generator
srand(time(NULL));
// initialize the array of lottery numbers
int lottery[5];
std::cout << "Your Lottery:" << std::endl;
// loop until array has no duplicates
bool unique = false;
while(!unique) {
int no_of_duplicates = 0;
// create five random numbers from 1-20 and store them
for(int i=0; i<5; i++)
lottery[i] = rand()%20+1;
// walk through lottery looking for duplicates
for(int i=0; i<5; i++) {
for(int ii=0; ii<5; ii++) {
// if there is a duplicate, increase the counter
if((lottery[ii]==lottery[i]) && (i != ii))
no_of_duplicates++;
}
}
// if there are no duplicates, set unique to true
// to get out of the while loop
if(!no_of_duplicates)
unique = true;
}
// display the array
for(int i = 0; i < 5; i++)
std::cout << lottery[i] << " ";
return 0;
}