Advertisement
Guest User

student_id.c

a guest
Sep 25th, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.37 KB | None | 0 0
  1. /*
  2.  
  3. [ student_id.c ]
  4. Author : AlexZ ( alessandro.suglia@gmail.com )
  5. Date : 22/09/2011 ( 19:29 PM )
  6.  
  7. HOW TO CHALLENGE THE LUCKY
  8. Take your student number ID and write
  9. a simple and easy C program that is able
  10. to mingle together all the possible number starting
  11. from that number that you've used.
  12. */
  13.  
  14. /* <------------ Header Definitions -----------------> */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <time.h>
  18.  
  19. /* < ----------- Macros definition ---------------------->*/
  20. #define TRUE 1
  21. #define FALSE 0
  22.  
  23. /* <----------- Functions Declarations ------------------> */
  24. int gen_num( int[], int );
  25. int check_id ( int[], int, int );
  26. void printArray( int[], int );
  27. int pow_fun( int, int );
  28.  
  29. /* <-------------- main.c -------------------------> */
  30. int main()
  31. {
  32.     int student_id[] = {5,8,7,1,7,6}; /* YOUR STUDENT ID divided in cifres */
  33.     int num_count = 0; /* simple variable in order to hold the value of the counter used to count the number of numbers created */
  34.     int len = sizeof(student_id)/sizeof(int); /* student_id's length */
  35.     int val = 0; /* temporary variable needed to hold the value generated by gen_num() */
  36.     int *temp; /* temp array in order to hold the new values */
  37.     int i;
  38.  
  39.     temp = (int*)malloc( len * sizeof(int));
  40.  
  41.     /* Verify if enough memory was allocated for the temp array */
  42.     if ( !(temp) )
  43.         {
  44.             perror("Malloc ERROR : Fatal..\n");
  45.             exit(-1);
  46.         }
  47.     /* Inizialization of the temp array */
  48.     for ( i = 0; i < len; i++ )
  49.       temp[i] = 0;
  50.  
  51.     printf("THE NUMBERS THAT YOU WILL USE TO THE LOTTERY : \n");
  52.  
  53.     while ( num_count < 6 ) /* If num_count is lower than six, do something.. needed to create six random number */
  54.     {
  55.       val = gen_num(student_id, len);
  56.       if ( !check_id(temp, len, val) ) /* check if the current random number is already present into the array, if not, put that value into the array and go on */
  57.     {
  58.       temp[num_count] = val;
  59.       num_count++;
  60.     }
  61.  
  62.     }
  63.  
  64.     printf("\n");
  65.     printArray(temp, len);
  66.  
  67.     printf("\n");
  68.    
  69.     free(temp);
  70.  
  71.     return EXIT_SUCCESS;
  72. }
  73.  
  74. /* <-------------- Functions Definition ------------------------ > */
  75.  
  76. /* ASIOTPF : Another Stupid Implementation Of The Pow Function. Too much resorse will be needed in order to link the whole math.h header*/
  77. int pow_fun ( int base, int esp )
  78. {
  79.   int i;
  80.   int res = 1;
  81.  
  82.   for ( i = 0; i < esp; i++ )
  83.     res *= base;
  84.  
  85.   return res;
  86. }
  87. /* check if the int num value is present into the a array, it returns TRUE if some value of the array match with num, FALSE in the opposite case*/
  88. int check_id ( int a[], int len, int num )
  89. {
  90.     int i;
  91.    
  92.     for ( i = 0; i < len; i++ )
  93.         if ( a[i] == num )
  94.       return TRUE;
  95.  
  96.     return FALSE;
  97. }
  98. /* return a random number starting from the STUDENT_ID specified */
  99. int gen_num( int a[], int len )
  100. {
  101.   int n_digit;
  102.   int pos;
  103.   int i;
  104.   int val = 0;
  105.  
  106.   srand(time(NULL));
  107.  
  108.   n_cifres = 1 + rand() % 2;
  109.    
  110.   for ( i = 0; i < n_digit; i++ )
  111.     {
  112.       pos = rand() % len;
  113.       val += a[pos] * pow_fun(10,i);
  114.     }
  115.  
  116.   return val;
  117.  
  118. }
  119. /* Simple function used in order to print to video the array value*/
  120. void printArray( int a[], int len )
  121. {
  122.   int i;
  123.  
  124.   for ( i = 0; i < len; i++)
  125.     printf("[%d]\t", i+1);
  126.   printf("\n");
  127.   for ( i = 0; i < len; i++ )
  128.     printf("%d\t", a[i]);
  129.   printf("\n");
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement