Question

This lab, for which you may work in groups of two, will require you to use...

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.

          

To manage your in-memory database, you will need to use some pointers to hold locations of your data. Please take some time to write down examples of where you will need to have pointers. You will need to have at least a pointer to the beginning of your database memory, another to show which record you are on, but think about the need for other pointers when you think about functions that will delete a record, add a record, etc.

One of the major points of this assignment is not just the ability to manage records in memory, it is also about how to manage the memory itself. There is a huge inherent danger in how memory is allocated and subsequently not released properly which will create a “memory leak” in your program which will consume memory over time and crash your system (due to all the memory being used, or your process hitting a max limit of memory). This will mean that you will need to manage how pointers are pointing at data very carefully as it is very easy to create a memory leak and your program will not crash, it will just not release memory properly.

          

You will need a menu to prompt users for the above requirements that may look like:

MENU

=======

1. Print all records

2. Print number of records

3. Print size of database

4. Add record

5. Delete record

6. Print number of accesses to database

7. Exit

     

Once you have gathered the appropriate information you will need to manipulate the data to hold the data correctly, but we are not using File I/O to maintain state on the data (would require too much time). Your database is a memory only database, so once your program ends, your database is gone. Being this is the case, it will be important to create a header file that has some data in it (5-7 records), so you will not need to enter all the records every time.

          

You will need to create a design doc as you did with project #1 that describes the reason for the program, the data type and how it will be used (also note the static type for number of accesses to your database) as well as testing criteria.

          

For this assignment you will need to use “script” to capture you testing your test cases while using your program. Script is a program that will capture everything you do on your screen until you exit the script program. To enter the script program simply type in :

          

$ script myProject2.out

          

This will start copying everything you type (stdin) as well as everything that goes to the screen until you type in exit at the $ prompt (you are actually in a new shell, so you won’t get logged out)..

       

Please submit all source/text/script files via the Project #2 Assignment in Moodle (only use zip or tar/tar.gz - no rar).

          

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;

}

Add a comment
Know the answer?
Add Answer to:
This lab, for which you may work in groups of two, will require you to use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

  • Hello all, I have a c++/unix (bash) question. I am struggling on starting this assignment. If...

    Hello all, I have a c++/unix (bash) question. I am struggling on starting this assignment. If you could start the assignment and tell me how to do the rest it would be greatly appreciated! (Quick thumbs up answer response if thorough and correct) Maintain automobile records in a database Write a shell script to create, view and modify a simple database that contains automobile records. The shell script has to be done in Bourne shell syntax (bash as a matter...

  • This project will create a GUI to access data held in a MySQL database. You will design a GUI usi...

    This project will create a GUI to access data held in a MySQL database. You will design a GUI using Swing components that will allow you to add a record, delete a record, update a record and display all current records int he database. The design is your choice. Extra credit will be given for the ability to search for a specific record by a key value. You will need to provide details of the database domain, table and record...

  • Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like...

    Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like ones in attachment ab Ass Part 1 sing Static Arra Do this part on your own. Write a program named lab11 a.c containing the following function: preconditions arc is terminated by dest is big enough hold arc postconditions dest contains src and is terminated by "10" void my strcpy (char dest const char srclj) This function will take two character arrays as parameters. The...

  • Looking for some help with this Java program I am totally lost on how to go about this. You have been approached by a local grocery store to write a program that will track the progress of their sale...

    Looking for some help with this Java program I am totally lost on how to go about this. You have been approached by a local grocery store to write a program that will track the progress of their sales people (SalesPerson.java) The information needed are: D Number integer Month 1 Sales Amount-Double Month 2 Sales Amount-Double Month 3 Sales Amount-Double Write a Java program that allows you to store the information of any salesperson up to 20 While the user...

  • C++. Please note the BOLDED ITEMS. You will create a simple linked structure. You will use...

    C++. Please note the BOLDED ITEMS. You will create a simple linked structure. You will use a simple node that has a pointer to a Node. The data for the Node will be a single data member of type char. You will build a structure where the last node you add will point to the first node created, i.e. it will be a circular linked structure. You will create a program that stores characters provided by the user, stored in...

  • Please i need the .H, .cpp and main.cpp files. Please use vector. its for my midterm so correct code that can run only....

    Please i need the .H, .cpp and main.cpp files. Please use vector. its for my midterm so correct code that can run only. Thank you. Pointers and Dynamic Memory – Chapter 11 1. Write code using pointers and the Accounts class from week 1 assignment as follows – create a program in which you a. Create 10 accounts using an array with id’s 0 to 9 and refer to each account using pointer notation b. Add an initial balance of...

  • Overview: Database management plays an integral role in nearly every area of business. Databases house customer, accoun...

    Overview: Database management plays an integral role in nearly every area of business. Databases house customer, accounting, and employee data, and these different data sets must all be efficiently managed in order to make the data accessible. Companies rely on database engineers to ensure that their records are accurate, updated, and tracked in real time. This course covers structured query language (SQL) and how it can be used to manage database schemas, manipulate data, and analyze data. For your final...

  • Description On the bottom of pg. 176, there is a table showing the various functions available...

    Description On the bottom of pg. 176, there is a table showing the various functions available to allocate and deallocate memory (and/or create/destroy objects) in C and C++. Your assignment is to write a program that demonstrates correct use of each of these (EXCLUDING aligned_alloc() and alloca(), which we did not discuss in class). Your program should do enough to demonstrate that each call works successfully. You should also include some comments to describe briefly the details of each call,...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT