



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Static allocation : Array size allocation arrayd[5] Automatic allocation : space for variables in a function. Memory gets freed when the function returns. Dynamic Allocation : Acquire memory when needed during runtime. The type void* int *ip; then ip points to an integer, char *cp; Here cp points to a character void *vp; here vp could point to any type. It allows functions like malloc to return general pointers whose actual types can be fixed later. void *malloc(sizeof( int)); Suppose we form a dynamic array to hold n floating point variables. The following statements would do the job: double darray; darray = malloc(n sizeof(double)); Here is an example of dynamic allocation of an array.
The state of Florida has a system that automatically creates a file that contains the name of each ticket purchaser along with the combination of 6 distinct numbers picked by that person. The job is to use this file in determining everyone's winnings. In particular, here is the payout for matching a certain number of values: Numbers Matched Winnings 3 $ 4 $ 5 $ 6 $ The program will ask the user for an input file with the data for the ticket purchases. Then the program will ask the user for the winning combination of numbers. (When playing the Florida lottery, you must pick 6 distinct numbers from the set {1,2,3,...,52,53}.) The user MUST enter these numbers in ascending order. Once these have been entered, the program should print out the names of all the winners, along with how much money they have won. It is guaranteed that each person playing the lottery has bought EXACTLY one ticket and is listed once in the file. It is desired that the implementation adheres to the following guidelines:
//Preconditions: file should be name of file storing the ticket buyer //information in proper format. //Postconditions: Returns a Pointer to the array storing all ticket buyer //information and total number of ticket buyers. gambler *read_file (char file[], int *numbuyers) { FILE *fp; int i, j; gambler list ; fp = fopen( file, "r"); fscanf ( fp, "%d", numbuyers); list = (gambler ) malloc( ( *numbuyers) sizeof(gambler)); // read in individual ticket buyer information for (i=0; i<numbuyers; i++) { fscanf( fp, "%s" , list[i].last ); fscanf( fp, "%s", list[i].first); for( j = 0; j<6; j++) fscanf( fp, "%d" ,&( list[i].nos[j] )); } fclose(fp); return list; } // FUNCTION looks at the LIST and gets winning numbers from the user void print_winners( gambler *list, int numtickets ) { int i, k , win_nos[6]; //get the winning lottery numbers from the user printf("\nEnter the winning lottery nos :\n "); for( i=0 ;i<6 ;i++) scanf("%d",&win_nos[i]); //for each ticket buyer , match the numbers with the winning numbers.
for ( k = 0; k< numtickets; k++) match_one( list [k ] , win_nos ); } // FUNCTION TO MATCH DATA void match_one (gambler buyer , int win_nos[] ) { int i=0 , j=0, count=0; while(i<6 && j<6) { if( win_nos[i] < buyer.nos[j] ) i++; else if (win_nos[i] > buyer.nos[j] ) j++; else { count++; i++; j++; } } //Depending on number matched print out the result switch(count) { case 3: printf( "\n%s %s matched 3 numbers and won $10.
n", buyer.first, buyer.last); break; case 4: printf( "\n%s %s matched 4 numbers and won $100.
n", buyer.first, buyer.last); break; case 5: printf( "\n%s %s matched 5 numbers and won $10000.\n", buyer.first, buyer.last); break;
grades = (int)malloc(students * sizeof(int)); for(i=0; i<students; i++) grades[i] = (int)malloc(exams * sizeof(int));