Advertisement
Guest User

student_id.c

a guest
Sep 24th, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.22 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.     return EXIT_SUCCESS;
  70. }
  71.  
  72. /* <-------------- Functions Definition ------------------------ > */
  73.  
  74. /* ASIOTPF : Another Stupid Implementation Of The Pow Function. Too much resorse will be needed in order to link the whole math.h header*/
  75. int pow_fun ( int base, int esp )
  76. {
  77.   int i;
  78.   int res = 1;
  79.  
  80.   for ( i = 0; i < esp; i++ )
  81.     res *= base;
  82.  
  83.   return res;
  84. }
  85. /* 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*/
  86. int check_id ( int a[], int len, int num )
  87. {
  88.     int i;
  89.    
  90.     for ( i = 0; i < len; i++ )
  91.         if ( a[i] == num )
  92.       return TRUE;
  93.  
  94.     return FALSE;
  95. }
  96. /* return a casual number starting from the STUDENT_ID specified */
  97. int gen_num( int a[], int len )
  98. {
  99.   int n_cifres;
  100.   int pos;
  101.   int i;
  102.   int val = 0;
  103.  
  104.   srand(time(NULL));
  105.  
  106.   n_cifres = 1 + rand() % 2;
  107.    
  108.   for ( i = 0; i < n_cifres; i++ )
  109.     {
  110.       pos = rand() % len;
  111.       val += a[pos] * pow_fun(10,i);
  112.     }
  113.  
  114.   return val;
  115.  
  116. }
  117. /* Simple function used in order to print to video the array value*/
  118. void printArray( int a[], int len )
  119. {
  120.   int i;
  121.  
  122.   for ( i = 0; i < len; i++)
  123.     printf("[%d]\t", i+1);
  124.   printf("\n");
  125.   for ( i = 0; i < len; i++ )
  126.     printf("%d\t", a[i]);
  127.   printf("\n");
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement