Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in c in c Create a monster struct that contains the following fields: name (a char array of size 32) - type (a char array

in c
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
in c
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Create a monster struct that contains the following fields: name (a char array of size 32) - type (a char array of size 16) - hp (an int) - tevel) (an int) This struct should be defined in a header file called Make sure you include a header guard. In monster.c, implement a function named create_monster that prompts the user to enter the values for each: field of the struct monster. The function should retutin a struct monster object. Additionally, create a function called print_monster that takes a struct monster as a parameter and prints the data in the following format: Nane: name Type: type HP: hp Level: level Include the declarations for these functions in monster. h. Create a third file named that includes monster. h and tests your and print_monster function. Now that the monster is defined and we can add new entries in memory, we need to be able to save the data toa file. Create a function named save_monster that takes a struct monster and a FILE pointer as parameters. The function should write the conster as binary date. You can assume that the given (FILE pointer is already open for writing. Deline this function in conster. c and include the declaration in Create a third file named test_save_monster.c) that includes conster.h and tests your save monster funetion by creating a conster with create_monster and saving it to a file. Make sure to open the file in append mode so that new entries will save to the end of the file. The next step is to load the data from the file into memory. Create a function named foad_monster that takes a FILE pointer as a parameter. The function should read the next line from the file and return a struct monster object. You can assume that the given (FILE pointer is already open for reading. Since this data is stored in a binary file, you will need to use the fread function to load the data. Since the dats was written in the same order that it was read, you can use the same (fread) calls to load the data into a struct monster array. You can assume that the file contains less than 128 monster objects. We will also need a way to display the data after loading. Create a function named print_-ionsters that takes an array of struct monster objects and the number of elements in the array as parameters. It should print a table of the data in the following format: \begin{tabular}{lll} Nane & Type & HP Level \\ \hline name1 & type1 & hp1 level1 \\ nane2 & typez & hp2 level2 \\ ... & & \end{tabular} Create a file named test_Load_monsters.c that includes monster.h and tests your Load_monster and print_consters functions by loading the data from the file and printing it to the screen. The included code examples use standard array indexing notation. Convert the indexing notation to an equivalent program using pointer arithmetic. Array initializations do not need to be converted since they define the anray memory. For array_utits.c) and array_tests. c, you can verify that it is working by compiling both of the fliles together. For example, you can complle with the following command: gcc0 array_tests array_utils.c array_tests.c C amaytests.c x Y_utils.c > *inctude "array_utits.h" int insert(int vatue, int index, int arrti, int size) f if (index se MAX SIZEE) \& printf("WAENTNo] Invatid index to inserto (n) s return stzeb II Append if (index = size) t arr [size] = vatues return size +1i if (index = index ){ arr[1+1]=arr[1]; arr[ index ]= value; return size +1; int renove val(int index, int arr[], int size) \{ for ( int 1= index; 1. 1 ifndef ARRAY_UUIL_H_ inctude estdio,hs define MAX_SIZE 10 int insert(int, int, int[1, int): int removesval(int, int [1, int): mendif II ARRAY_UILS_H Create a wonster struct that contains the following flelds! - name (a char array of size 32) - type (a char array of size 16) - hp (an int) - Lever (an int) This struct should be defined in a header file called monster. h. Make sure you include a header guard. In monster.c, implement a function named create_monster that prompts the user to enter the values for each field of the struct monster. The function should return a struct monster object. Additionally, create a function called print_monster that takes a struct monster as a parameter and prints the data in the following format: Nane: name Type: type HP: hp Level: level Include the declarations for these functions in Create a third file named that includes and tests your and Now that the monster is deffined and we can add new entries in memory, we need to be able to save the data to a file. Create a function named (save.monster that takes a struct nonster and a Fite pointer as parameters. The function should write the coonster as binary data. You can assume that the elven FFLE pointer is already open for writing. Dofine this funetion in conster. c and include the declaration in Eenster. h. Create a third file named test_save_monster. c that includes monster.h and tests your save monster function by creating a monster with create monster and saving it to a file. Make sure to open the file in append mode so that new entries will save to the end of the file. The next step is to load the data from the fille into memory, Create a function hamed foadjnonster that takes a FrLE pointer as a parameter. The function should read the next line from the file and returf a struct monster object. You can assume that the given (FILE pointer is already open for feading. Since this data is stored in a binary fille, you will need to use the fread function to load the data, Since the date was written in the same order that it was read, you can use the same fread calls to load the data inte a struct ijonster array. You can assume that the file contains less than 128 monster objects. We will also need a way to display the data after loading. Create a function named print mionsters that fakes an array of struct monster objects and the number of elements in the array as parameters. It should print a table of the data in the following format: Nanenamelnane?*.sTypetype2typezHPLevelhp1Level1hp2level2 Createa file named test_load_Eonsters. c that includes monster. h and tests your and print monsters functions by loading the data from the file and printing it to the screen. The included code examples use standard array indexing notation. Convert the indexing notaflon to an equivalent program using pointer arithmetic. Array intitializations do not need to be converted since they define the array memong. For array_utils.c and array_tests.c, you can verify that it is working by compiling both of the flles together. For example, you can complle array_utits.c and with the following command: gccto array_tests array_ut1ts.c array_tests.c y_utils.c > ... 1/ this is array_utils.c \#include "array_utits.h" int insert(int vatue, int index, int arrl1, int size) \{ if (Index se MAX SIZE) \{ printf(" [WAPNtwol tnvatid index to insert, (n)/ return sizel 3 II Append if (andex en size) } arrisize] = vatue return size +13 If (ndex e size) \{ int 1=512e=14 white (1 se index) \{ arr{t+1}=arr[1]} see? arr [index] = value; return s12e+1i. 3 int renove_val(int index, int arr[l, int size) \{ for ( int i= index; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Entity Alignment Concepts Recent Advances And Novel Approaches

Authors: Xiang Zhao ,Weixin Zeng ,Jiuyang Tang

1st Edition

9819942527, 978-9819942527

More Books

Students also viewed these Databases questions

Question

Why does sin 2x + cos2x =1 ?

Answered: 1 week ago

Question

What are DNA and RNA and what is the difference between them?

Answered: 1 week ago

Question

Why do living creatures die? Can it be proved that they are reborn?

Answered: 1 week ago

Question

Address an envelope properly.

Answered: 1 week ago

Question

Discuss guidelines for ethical business communication.

Answered: 1 week ago