Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C programming help! (Only use C and nothing else) Problem: Help the Monster Trainer! You are a genius programmer and learned dynamic memory allocation. Now

C programming help! (Only use C and nothing else)

image text in transcribedimage text in transcribedimage text in transcribed

Problem: Help the Monster Trainer! You are a genius programmer and learned dynamic memory allocation. Now it is time to show your expertise on this topic. Several monster trainers have come to you for advice regarding expeditions they're planning into various regions. You are writing a program to estimate how many monsters they can expect to capture in each region. - You've got a Small Monster Index that tells you the name, type, and relative commonality of all the small monsters in question. - (A monster's absolute commonality is the same in each region. A monster's relative commonality will change region by region as calculations are performed - we'll show you how that works shortly.) - You've also got an atlas that tells you about the relevant regions and which small monsters are present in them. - Each trainer tells you which regions they're visiting, and how many monsters they intend to capture per region. - To estimate the number of a given monster M, a trainer will capture in a region R : - Divide the relative population of M in R by R 's total relative population. - Multiply the result by the total number of captures the trainer intends per region. - Round this result to the nearest integer. .5 rounds up, so you can use round( and its friends. Note that this can result in a total slightly different than the trainer intended! Data Structures The structures you'l1 use for the monsters, regions, itineraries, and trainers are shown below. You must use these structures in your code. However, you are free to add more structures if you need. (Some of my unit test case may depends on this structure and members and you should not change the structure name and member name) You'll need to allocate, read, compute upon, output from, and subsequently free: - The monster index. - The names and elements of each monster in the monster index. - The region atlas. - The names and monster lists of each region. - A list of trainers. - The names and itineraries of each trainer. - The region list of each itinerary. Input Specification (standard input - No file I/O) Use regular scanf to take inputs. Your code should not use any file i/o, such as fopen, fscanf, etc., are not relevant for this assignment. The input will be taken from standard console input. Read the commands mentioned at the end of the assignment description to see how can you pass input to your code as the input size could be large. In the input files, there can be blank lines to make them more readable. They may or may not be present in the actual inputs; you should completely ignore blank lines. If you use regular scanf, those blank lines will be automatically ignored. You can just write your code thinking that the line gaps do not exist. The first line of the input contains the overall different number of monsters mcount . The next mcount lines contain information of monsters where each line contains two strings and one integer. The first string is the monster name (single word string with maximum length is 50). The second string is the element of the monster (single word string with maximum length is 50 ). The integer represents the population of the monster (max value is 1 million) After that the input contains the number of regions rcount. After that the input contains information about rcount number of regions. Where the first line of a region contains the name of the region, next line contains the number of different monsters rmcount in that region with the word monsters. The next rmcount lines contain the name of different monsters in the region. After the rcount number of regions, the input contains number of trainers tcount. Then tcount number of trainers' information is provided. For each trainer the first line represents the trainer name (single word string with maximum length is 50), the next line contains how many captures with the word captures and then then next line contains number regions trcount with the word regions. After that the trcount lines contain the name of the region the trainer is visiting. Output Specification The output of the program must be written to standard console output (no file i/o such as fprintf should be used). The output should be in the exact format as specified in the sample output. See the commands below at the end of the assignment description on how to write the result of your code into a file using command line. This will help you to compare your result with the actual result. The output contains each trainer name with the number of monster the trainer will capture from each region. Follow the output format as shown in the sample output. Note that we will test your code with diff command to match your output file with our output file. If they do not exactly match, you will loss significant grade for that specific test case. But there maybe some partial credit depending on how much it is not matching. Print order should generally be consistent with input: - Print the trainers in the order you got them. - Within the trainers, print the regions in the order you got the visits. - Within the regions, print the monster counts in the order they show up in the atlas for that region. - Print blank lines between each trainer. Specific Requirements - You have to use dynamic memory allocation - You have to use the provided structures without changing their name - You may not use global variables - You are not allowed to use VLA - You are not allowed to create same monster and regions multiple times. Try to find a technique to re-use the created monsters and regions. - You have to implement at least the following functions: - monster* makeMonster(char *name, char *element, int population): This function returns a dynamically allocated monster filled with the provided parameters - monster** readMonsters(int *monstercount): This function returns an array of monster pointers where each monster pointer points to the dynamically allocated monsters with fill-up information from the provided inputs. It can use the makeMonster function in this process. This function also updates the passed variable reference pointed by monsterCount so that the caller to this function knows how many monsters are returned from the function. - region** readRegions(int *countRegions, monster** monsterlist, int monstercount): This function returns an array of region pointers where each region pointer points to a dynamically allocated region, filled up with the information from the inputs, and the region's monsters member points to an appropriate list of monsters from the monsterList passed to this function. This function also updates the passed variable reference pointed by countRegions (to inform the caller about this count). As the loadMonsters function has created all the monsters using dynamic memory allocation, you are getting this feature to use/reuse those monsters in this process. - trainer* loadTrainers(int *trainercount, region** regionlist, int countRegions): This function returns a dynamically allocated array of trainers, filled up with the information from the inputse, and the trainer's visits field points to a dynamically allocated itinerary which is filled based on the passed regionList. This function also updates the passed variable reference pointed by trainerCount. As the loadRegions function has crated all the regions using dynamic memory allocation, you are getting this feature to use/re-use those regions in this process. - void processinputs(monster** monsterlist, int monstercount, region** regionlist, int regionCount, trainer* trainerList, int trainercount): This function processes all the data and produce the output. During this process, you can create/use more functions if you want. - void releaseMemory(monster** monsterList, int monstercount, region** regionList, int regioncount, trainer* trainerList, int trainercount ): This function takes all the dynamically allocated arrays and free-up all the memory. You can create/use more function in this process if you want. - You have to use memory leak detector code as shown the lab as well as explained in webcourses - You must \#include "leak_detector_c.h" in your code, and - You must call atexit(report_mem_leak) as the first line of your main(). - leak_detector_c.h and leak_detector_c.c are available in webcourse. - You do not need to comment line by line, but comment every function and every "paragraph" of code. - You don't have to hold any particular indentation standard, but you must indent and you must do so consistently within your own code

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_2

Step: 3

blur-text-image_3

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

101 Database Exercises Text Workbook

Authors: McGraw-Hill

2nd Edition

0028007484, 978-0028007489

More Books

Students also viewed these Databases questions