summaryrefslogtreecommitdiff
path: root/reference/C/PROBLEMS/lotto2.c
blob: c8f7474640e32207a72f06b6bf8e3b0fb72ffa39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/************************************************************************
 *
 * Purpose: Second crack at a lottery number selector.
 * Author:  M.J. Leslie.
 * Date:   03-Dec-94
 * Description: Any 6 random numbers from 1 to 49 are displayed. 
 *              Duplicates are removed and the results sorted.
 *
 ************************************************************************/

/********** Preprocessor  ***********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUMBERS 6

/********** Functions ***************************************************/

void display_nums(int *, int);
int comp_nums(const int *, const int *);

/************************************************************************/
main()
   {
   int num[NUMBERS];			/* array holding the selected
					 * numbers			*/
   int count;				/* General  counter		*/
		

   printf("\n\nLOTTERY CRACKER V1.1\n");
   printf("--------------------\n\n");
   printf("\tPossible winning ticket number is ");

					/* New starting point
					 * for the random number generator.
					 */

   srand((unsigned int)time((time_t *)NULL));

					/* Collect Six numbers.	
					 * Filter out duplicats as we go.
					 */
   {
   int dup, count2;
   for (count=0; count < NUMBERS; count++) 
      {
      num[count]=(rand()%49)+1;		/* Get a number.		*/
      dup=0; 				/* Set the duplicate flag to 
					 * NO DUPLICATES		*/
					/* Test latest number with the ones
				 	 * already held.		*/
      for (count2=0 ; count2 < count; count2++)
         {
					/* If match found - set flag.	*/
         if (num[count] == num[count2]) dup++ ;
         }
					/* If flag set - reset the array
					 * index so we overwrite the current
					 * number.			*/
      if (dup > 0 ) count--;
      }
   }
					/* Sort the numbers		*/
   qsort(
	num, 				/* Pointer to elements		*/
	NUMBERS, 			/* Number of elements		*/
	sizeof(int),  			/* size of one element.		*/
	(void *)comp_nums		/* Pointer to comparison function */
	);

   display_nums(num, NUMBERS); 		/* Display the results		*/
   }

/************************************************************************
 *
 * comp_nums: Compare two numbers.
 *
 ************************************************************************/

int comp_nums(const int *num1, const int *num2)
   {
   return (*num1 - *num2);
   }

/************************************************************************
 *
 * display_nums: Display the numbers
 *
 ************************************************************************/

void display_nums(int *array, int count)
   {
					/* Print all the elements of 
					 * the array.               	*/
   while ( count-- ) 
      {
      printf("%d ",*array);
      array++;
      }
   puts("");
   puts("");
   }