Advertisement
Guest User

student_id.c

a guest
Sep 23rd, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 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;
  37.  
  38.     temp = (int*)malloc( len * sizeof(int));
  39.  
  40.     if ( !(temp) )
  41.         {
  42.             perror("Malloc ERROR : Fatal..\n");
  43.             exit(-1);
  44.         }
  45.  
  46.     for ( i = 0; i < len; i++ )
  47.       temp[i] = 0;
  48.  
  49.     srand(time(NULL));
  50.  
  51.     printf("THE NUMBERS THAT YOU WILL USE TO THE LOTTERY : \n");
  52.  
  53.     while ( num_count < 6 )
  54.     {
  55.         n_cifres = 1 + rand() % 2;
  56.  
  57.     turn:
  58.         for ( i = 0; i < n_cifres; i++ )
  59.         {
  60.             pos = rand() % len;
  61.  
  62.         if( !check_id( temp, len, pow_fun(student_id[pos], i) ) )
  63.           temp[num_count] += ( student_id[pos] * pow_fun(10, i) );
  64.         else
  65.           goto turn;
  66.  
  67.  
  68.  
  69.         }
  70.         printf("\t");
  71.         num_count++;
  72.  
  73.  
  74.     }
  75.  
  76.     printf("\n");
  77.     printArray(temp, len);
  78.  
  79.     printf("\n");
  80.  
  81.     return EXIT_SUCCESS;
  82. }
  83. /* ASIOTPF : Another Stupid Implementation Of The Pow Function. Too much resorse will be needed in order to link the whole math.h header*/
  84. int pow_fun ( int base, int esp )
  85. {
  86.   int i;
  87.   int res = 1;
  88.  
  89.   for ( i = 0; i < esp; i++ )
  90.     res *= base;
  91.  
  92.   return res;
  93. }
  94. /* 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*/
  95. int check_id ( int a[], int len, int num )
  96. {
  97.     int i;
  98.  
  99.     for ( i = 0; i < len; i++ )
  100.         if ( a[i] == num )
  101.       return TRUE;
  102.  
  103.     return FALSE;
  104. }
  105.  
  106. /* Simple function used in order to print to video the array value*/
  107. void printArray( int a[], int len )
  108. {
  109.   int i;
  110.  
  111.   for ( i = 0; i < len; i++)
  112.     printf("[%d]\t", i+1);
  113.   printf("\n");
  114.   for ( i = 0; i < len; i++ )
  115.     printf("%d\t", a[i]);
  116.   printf("\n");
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement