Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use C language. This lab, for which you may work in groups of two, will r

ID: 3756316 • Letter: P

Question

Please use C language.

This lab, for which you may work in groups of two, will require you to use a C structure to hold a record of any kind of data, i.e., address book, library of books as with chapter 14, etc. These records will be held in dynamic memory using memory allocation (malloc) to create new memory and the function (free) to release memory created via (malloc). Do not use linked lists for this assignment, we are using dynamic memory allocation to hold records, not using linked lists The structures will act as an in memory database for your records (structures), and you will need to create enough memory to hold all of your records, but also when you remove records, you will need to allocate new memory, move the data over to that memory space, and free the old memory (instead of using a static size array). No use of arrays is allowed to hold your structures, but you can use arrays in other places in your program Your program will prompt for information that will be added to a new structure that will be added when you call the add function. Delete is as simple as just taking the last record out of your database (i.e. no need to search for a record" to delete - this assignment is about pointers and memory allocation /free, so no need to make the algorithm more complicated) In addition to your memory to hold your data / structures, your program will also need to hold a static duration memory type that will serve as the counter for how many times the database has changed. Along with the amount of times the database has changed, you will also need to have a variable to hold the number of records in your database, functions to calculate size (records multiplied by sizeof struct), and functions to add, print, and delete records. You will not be required to use lookup functions, print is just the entire database.

Explanation / Answer

Program code:

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

#define Maximum_record 50

struct Library

{

char book_name[40];

int no_title;

Library *ptr;

}typedef Library;

Library *book_record[Maximum_record];

int last_record = -1;

int record_size = 0;

int count = 0;

void Display_numberof_Records()

{

printf("Total book records = %d ", (last_record+1));

}

void getSize()

{

printf("Total database size : %d ", sizeof(Library)*(last_record+1));

}

void display_record()

{

int i;

printf("Book_Name | Title_number ");

printf("------------------------------ ");

for(i = 0; i <= last_record; i++)

{

printf("%s %d ",book_record[i]->book_name, book_record[i]->no_title);

count++;

}

}

void record_deletion()

{

if(last_record<0)

{

printf(" No record to delete!!!!!!");

}

else

{

free(book_record[last_record]);

last_record--;

}

}

void Insert_record()

{

Library newbook =(Library) malloc(sizeof(Library));

last_record++;

book_record[last_record] = newbook;

printf("Enter new book name : ");

scanf("%s", newbook->book_name);

printf("Enter number of books : ");

scanf("%d", &newbook->no_title);

printf("New database record inserted!!!!!!!!! ");

count++;

record_size = sizeof(Library) * (last_record+1);

}

int main()

{

int flag=1,ch;

last_record = -1;

while(flag==1)

{

printf(" Menu option");

printf(" 1.Print all records");

printf(" 2.Print number of records");

printf(" 3.Print size of database");

printf(" 4.Add record");

printf(" 5.Delete record");

printf(" 6.Exit");

printf(" Enter your choice: ");

scanf("%d", &ch);

switch (ch)

{

case 1:

display_record();

break;

case 2:

Display_numberof_Records();

break;

case 3:

getSize();

break;

case 4:

Insert_record();

break;

case 5:

record_deletion();

printf(" Record successfully deleted!!!!!!!!!!");

break;

case 6:

flag=0;

printf(" Existing!!!!!!");

break;

}

}

return 0;

}