Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please use C lenguage Problem: Help the Monster Trainer! You are a genius programmer and learned dynamic memory allocation. Now it is time to show

Please use C lenguage

image text in transcribedimage text in transcribedimage 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'll 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 stincture 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. - 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 1million) 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 moount 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. Example Input and Output Example Input Example Output (see the mapping example 8// this represents momber of aifferent monsters discussed in the next page to understand how StAugustine Grass 12 Zoysia Grass 8 this output is generated) WholeWheat Bread 6 Alice MultiGrain Bread 10 Rome Rye Bread 10 2-StAugustine Cinnamon Spice 5 1-Zoysia Pepper Spice 10 1-WholeWheat Pumpkin Spice 30 1-Pepper Aria 3 //this represents number of regions 1-Zoysia 1-MultiGrain Rome 1-Pepper 4 //this represents number of different monsters in that region 2-Pumpkin StAugustine \begin{tabular}{ll} Zoysia & Bob \\ WholeWheat & Rome \\ Pepper & 1 -StAugustine \\ \hline \end{tabular} 1-Zoysia Helve 1-WholeWheat 5//this represents momber of different monsters in that region 1-Pepper StAugustine Helve WholeWheat 1-StAugustine MultiGrain 1-WholeWheat Rye 1-MultiGrain Cinamon 1-Rye Aria Aria 1-Zoysia 5 //this represents momber of different monsters in that region 1-MultiGrain Zoysia 1-Pepper MultiGrain 2-Pumpkin Cinnamon Pepper Carol Pumpkin Aria 1-Zoysia 3 //this represents momber of trainers 2-MultiGrain 1-Cinnamon Alice 2-Pepper 5//this represents momber of captures 5-Pumpkin 2// this represents momber of different regions this training is visiting Rome Aria Bob 4/ / this represents mumber of captures 3//this represents momber of different regions this training is visiting Rome Helve Aria Carol 10/ /this represents number of captures 1 //this represents momber of different regions this training is visiting Aria Mapping Example Here's the table of how each indivicual trainer's results are computed. It also shows how rounding issues can lead to trainers capturing more monsters than they intend! \begin{tabular}{lrrrrrr} Rome & \multicolumn{1}{c}{ Raw } & Divided & Alice & Round & Bob & Round \\ Coefficient & 1.00 & 36.00 & 5.00 & & 4.00 & \\ StAugustine & 12.00 & 0.33 & 1.67 & 2.00 & 1.33 & 1.00 \\ Zoysia & 8.00 & 0.22 & 1.11 & 1.00 & 0.89 & 1.00 \\ WholeWheat & 6.00 & 0.17 & 0.83 & 1.00 & 0.67 & 1.00 \\ Pepper & 10.00 & 0.28 & 1.39 & 1.00 & 1.11 & 1.00 \\ Total & 36.00 & 1.00 & 5.00 & 5.00 & 4.00 & 4.00 \end{tabular} \begin{tabular}{lrrrr} Helve & \multicolumn{1}{c}{ Raw } & Divided & Bob & Round \\ Coefficient & 1.00 & 43.00 & 4.00 & \\ StAugustine & 12.00 & 0.28 & 1.12 & 1.00 \\ WholeWheat & 6.00 & 0.14 & 0.56 & 1.00 \\ MultiGrain & 10.00 & 0.23 & 0.93 & 1.00 \\ Rye & 10.00 & 0.23 & 0.93 & 1.00 \\ Cinnamon & 5.00 & 0.12 & 0.47 & 0.00 \\ Total & 43.00 & 1.00 & 4.00 & 4.00 \end{tabular} 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 0 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. 0 region** readRegions(int *countRegions, monster** monsterlist, int monster(ount): This function retuuns an array of region pointers where each region pointer points to a dynamically allocated region, filled up with the information from the imputs, 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 retums 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 0 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(). o 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. Rubric (subject to change): The code will be compiled and tested in CodeGrade Platform for grading. If your code does not compile in CodeGrade, we conclude that your code is not compiling and it will be graded accordingly. We will apply a set of test cases to check whether your code can produce the expected output or not. Failing each test case will reduce some grade based on the rubric given bellow. If you hardcode the output just to match particular test case, you will get 200% for the assignment. 1. If a code does not compile the code may get 0 . However, some partial credit maybe awarded. A code having compiler error cannot get more than 50% even most of the codes are correct 2. If you modify or do not use the required structure: get 0 3. Not using dynamic memory allocation for storing data will receive 0 4. There is no grade for a well indented and well commented code. But a bad indented code will receive 20% penalty. Not putting comment in some important block of code 10% 5. Implementing required functions and other requirements: 30% 6. Freeing up memory properly with zero memory leak (if all the required malloc implemented): (20\%) 7. Passing test cases: 50%

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

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

Analyse the various techniques of training and learning.

Answered: 1 week ago