For some reason I am totally lost, i literally understand everything until the part where "balls[i] == ball[j]", basically I dont want the same numbers repeated in the lottery........ but if ball[i] is assigned a random number and ball[j] isnt, how are they being compared!?
void lottery(int total_balls,int balls_to_draw)
{
// can't draw more balls then you have in the pool of balls to draw
if(total_balls < balls_to_draw)
return;
// initialize your pseudo random generator to the current seconds, if you run this function twice in the same second you'll get the same results!
// you are less likely to run into problems if you srand a single time (maybe in your main or a startup procedure)
srand(time(NULL));
// create a new array of balls... This is just incorrect and a poor display- use int balls[balls_to_draw]; instead of creating a new array...
int *balls = newint[balls_to_draw];
for(int i = 0; i < balls_to_draw;i++)
{
// request a random number between 0 - 48 in this example, you should be explicit with your order of operations here, it will work but parens would make intent obvious
balls[i] = rand() % total_balls + 1;
// we are still inside the other loop, but starting a second loop from 0 to the currently pulled ball so we can check if we have ever pulled that ball before, this is a super hacky way
// to make sure we don't pull the same ball again. i--; break; basically says redo this current outside loop because we already have that ball in the list. yuck.
for(int j = 0; j < i + 1; j++)
{
// do we already have this ball in the list?
if(balls[i] == balls[j] && i != j)
{
i--;
break;
}
// we don't already have this ball and have reached the current ball, yay?
elseif(j == i)
cout << balls[j] << endl;
}
}
// seriously don't do this. this is just bad practice, change this line if you change the int *balls = new ..... to balls[balls_to_draw];
delete[] balls;
}
thank you so MUCH FOR YOUR BREAK DOWN AND YOUR TIME. I guess I just need more practice for it to click to me. I'm going to do a review of some of the basics, maybe thats why i'm a little confused.
But thank you again...... I guess practice makes perfect.