Docsity

Dynamic Memory Allocation and Florida Lottery System, Study notes of Computer Science

Toggle sidebar
bg1
Dynamic Allocation of memory
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.
pf3
pf4
pf5

Partial preview of the text

Download Dynamic Memory Allocation and Florida Lottery System and more Study notes Computer Science in PDF only on Docsity!

Dynamic Allocation of memory

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 Florida Lottery System

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:

  1. Use a record to store information about each ticket.
  2. Dynamically allocate an array to store all the ticket information based on the first number in the file. Input File Format The first line will contain n , the total number of tickets bought. The next 2n lines will contain information about each ticket bought. The information for a single ticket will be on two lines. The first of the two lines will contain the last name of the ticket buyer, followed by the first name. The following line will contain the six integers chosen by that buyer. Here is a sample file, input.txt : 5 Dorr Robin 1 15 19 26 33 46 Mong Drian 17 19 33 34 46 47 Pate David 1 4 9 16 25 36 Bayer Lisa 17 19 34 46 47 48 Gupta Alok 5 10 17 19 34 47

//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));