From the course: C: Data Structures, Pointers, and File Systems

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Solution: Lotto simulation

Solution: Lotto simulation - C Tutorial

From the course: C: Data Structures, Pointers, and File Systems

Start my 1-month free trial

Solution: Lotto simulation

(upbeat music) - [Narrator] I hope you enjoyed this challenge. As with any programing problem, the lotto simulation can be solved in several ways. Here's my solution. The key to this challenge is to find a way to track the lotto balls already drawn. The method I use is to create and array lotto ball here at line 10, which has the same number of elements as the number of balls, or numbers available to draw, set as the constant expression count defined at line five. The array is initialized with all zeros in the for loop at line 16. The while loop at line 21 draws the balls. It plucks a random number into variable B at line 24. Then it compares that array element number with zero at line 26. So if the element is zero, the ball hasn't been drawn. It's a valid number. At line 29, the element is set to one, which prevents it from being drawn again, and the draw index is incremented, meaning there's one less number to draw. The loop repeats. Now if instead, the value of lotto ball B at line…

Contents