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
- /* <----------- Functions Declarations ------------------> */
- int gen_num( int[], int );
- 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 len = sizeof(student_id)/sizeof(int); /* student_id's length */
- int val = 0; /* temporary variable needed to hold the value generated by gen_num() */
- int *temp; /* temp array in order to hold the new values */
- int i;
- temp = (int*)malloc( len * sizeof(int));
- /* Verify if enough memory was allocated for the temp array */
- if ( !(temp) )
- {
- perror("Malloc ERROR : Fatal..\n");
- exit(-1);
- }
- /* Inizialization of the temp array */
- for ( i = 0; i < len; i++ )
- temp[i] = 0;
- printf("THE NUMBERS THAT YOU WILL USE TO THE LOTTERY : \n");
- while ( num_count < 6 ) /* If num_count is lower than six, do something.. needed to create six random number */
- {
- val = gen_num(student_id, len);
- 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 */
- {
- temp[num_count] = val;
- num_count++;
- }
- }
- printf("\n");
- printArray(temp, len);
- printf("\n");
- free(temp);
- return EXIT_SUCCESS;
- }
- /* <-------------- Functions Definition ------------------------ > */
- /* 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;
- }
- /* return a random number starting from the STUDENT_ID specified */
- int gen_num( int a[], int len )
- {
- int n_digit;
- int pos;
- int i;
- int val = 0;
- srand(time(NULL));
- n_cifres = 1 + rand() % 2;
- for ( i = 0; i < n_digit; i++ )
- {
- pos = rand() % len;
- val += a[pos] * pow_fun(10,i);
- }
- return val;
- }
- /* 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