Advertisement
Guest User

student_id.c

a guest
Sep 23rd, 2011
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 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. int check_id ( int[], int, int );
  24. void printArray( int[], int );
  25. int pow_fun( int, int );
  26.  
  27. /* <-------------- main.c -------------------------> */
  28. int main()
  29. {
  30.     int student_id[] = {5,8,7,1,7,6}; /* YOUR STUDENT ID divided in cifres */
  31.     int num_count = 0; /* simple variable in order to hold the value of the counter used to count the number of numbers created */
  32.     int pos; /* a kinda of variable to randomly use the cifres */
  33.     int n_cifres; /* variable used to hold the number of cifres that will be generated */
  34.     int len = sizeof(student_id)/sizeof(int);
  35.     int i;
  36.     int temp[len];
  37.  
  38.     for ( i = 0; i < len; i++ )
  39.       temp[i] = 0;
  40.  
  41.     srand(time(NULL));
  42.  
  43.     printf("THE NUMBERS THAT YOU WILL USE TO THE LOTTERY : \n");
  44.  
  45.     while ( num_count < 6 )
  46.     {
  47.         n_cifres = 1 + rand() % 2;
  48.    
  49.     turn:
  50.         for ( i = 1; i <= n_cifres; i++ )
  51.         {
  52.             pos = rand() % len;
  53.        
  54.         if( !check_id( temp, len, pow_fun(student_id[pos], i) ) )
  55.           temp[num_count] = pow_fun(student_id[pos], i);
  56.         else
  57.           goto turn;
  58.  
  59.        
  60.            
  61.         }
  62.         printf("\t");
  63.         num_count++;
  64.        
  65.  
  66.     }
  67.    
  68.     printf("\n");
  69.     printArray(temp, len);
  70.  
  71.     printf("\n");
  72.  
  73.     return EXIT_SUCCESS;
  74. }
  75. /* ASIOTPF : Another Stupid Implementation Of The Pow Function. Too much resorse will be needed in order to link the whole math.h header*/
  76. int pow_fun ( int base, int esp )
  77. {
  78.   int i;
  79.   int res = 1;
  80.  
  81.   for ( i = 0; i < esp; i++ )
  82.     res *= base;
  83.  
  84.   return res;
  85. }
  86. /* 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*/
  87. int check_id ( int a[], int len, int num )
  88. {
  89.     int i;
  90.  
  91.     for ( i = 0; i < len; i++ )
  92.         if ( a[i] == num )
  93.       return TRUE;
  94.    
  95.     return FALSE;
  96. }
  97.  
  98. /* Simple function used in order to print to video the array value*/
  99. void printArray( int a[], int len )
  100. {
  101.   int i;
  102.  
  103.   for ( i = 0; i < len; i++)
  104.     printf("[%d]\t", i+1);
  105.   printf("\n");
  106.   for ( i = 0; i < len; i++ )
  107.     printf("%d\t", a[i]);
  108.   printf("\n");
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement