Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- [ student_id.c ]
- Author : AlexZ ( alessandro.suglia@gmail.com )
- Date : 22/09/2011 ( 19:29 PM )
- HOW TO CHALLENGE THE LUCKY
- Take your student number ID and write
- a simple and easy C program that is able
- to mingle together all the possible number starting
- from that number that you've used.
- */
- /* <------------ Header Definitions -----------------> */
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- /* < ----------- Macros definition ---------------------->*/
- #define TRUE 1
- #define FALSE 0
- int check_id ( int[], int, int );
- void printArray( int[], int );
- int pow_fun( int, int );
- /* <-------------- main.c -------------------------> */
- int main()
- {
- int student_id[] = {5,8,7,1,7,6}; /* YOUR STUDENT ID divided in cifres */
- int num_count = 0; /* simple variable in order to hold the value of the counter used to count the number of numbers created */
- int pos; /* a kinda of variable to randomly use the cifres */
- int n_cifres; /* variable used to hold the number of cifres that will be generated */
- int len = sizeof(student_id)/sizeof(int);
- int i;
- int *temp;
- temp = (int*)malloc( len * sizeof(int));
- if ( !(temp) )
- {
- perror("Malloc ERROR : Fatal..\n");
- exit(-1);
- }
- for ( i = 0; i < len; i++ )
- temp[i] = 0;
- srand(time(NULL));
- printf("THE NUMBERS THAT YOU WILL USE TO THE LOTTERY : \n");
- while ( num_count < 6 )
- {
- n_cifres = 1 + rand() % 2;
- turn:
- for ( i = 0; i < n_cifres; i++ )
- {
- pos = rand() % len;
- if( !check_id( temp, len, pow_fun(student_id[pos], i) ) )
- temp[num_count] += ( student_id[pos] * pow_fun(10, i) );
- else
- goto turn;
- }
- printf("\t");
- num_count++;
- }
- printf("\n");
- printArray(temp, len);
- printf("\n");
- return EXIT_SUCCESS;
- }
- /* ASIOTPF : Another Stupid Implementation Of The Pow Function. Too much resorse will be needed in order to link the whole math.h header*/
- int pow_fun ( int base, int esp )
- {
- int i;
- int res = 1;
- for ( i = 0; i < esp; i++ )
- res *= base;
- return res;
- }
- /* 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*/
- int check_id ( int a[], int len, int num )
- {
- int i;
- for ( i = 0; i < len; i++ )
- if ( a[i] == num )
- return TRUE;
- return FALSE;
- }
- /* Simple function used in order to print to video the array value*/
- void printArray( int a[], int len )
- {
- int i;
- for ( i = 0; i < len; i++)
- printf("[%d]\t", i+1);
- printf("\n");
- for ( i = 0; i < len; i++ )
- printf("%d\t", a[i]);
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement