Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This assignment is intended to make you do a lot of work with dynamic memory allocation, pointers, and arrays of pointers. Don't wait until the

 This assignment is intended to make you do a lot of work with dynamic memory allocation, pointers, and arrays of pointers. Don\'t wait until the weekend it\'s due to start it! Your solution should follow a set of requirements to get credit. What should you submit? Write all the code in a single file main.c file. Submit your main.c file only. Please include the following commented lines in the beginning of your code to declare your authorship of the code: /* COP 3502C Assignment 1 This program is written by: Your Full Name */ Compliance with Rules: UCF Golden rules apply towards this assignment and submission. Assignment rules mentioned in syllabus, are also applied in this submission. The TA and Instructor can call any students for explaining any part of the code in order to better assess your authorship and for further clarification if needed. Caution!!! Sharing this assignment description (fully or partly) as well as your code (fully or partly) to anyone/anywhere is a violation of the policy. I may report to office of student conduct and an investigation can easily trace the student who shared/posted it. Also, getting a part of code from anywhere will be considered as cheating. Deadline: See the deadline in Webcourses. The assignment will accept late submission up to 24 hours after the due date time with 20% penalty. After that the assignment submission will be locked. An assignment submitted by email will not be graded and such emails will not be replied according to the course policy. What to do if you need clarification on the problem? I will create a discussion thread in webcourses and I highly encourage you to ask your question in the discussion board. Maybe many students might have same question like you. Also, other students can reply and you might get your answer faster. Also, you can write an email to the TAs and put the course teacher in the cc for clarification on the requirements. How to get help if you are stuck? According to the course policy, all the helps should be taken during office hours. Occasionally, we might reply in email. 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.
o (A monsters absolute commonality is the same in each region. A monsters relative commonality will change region by region as calculations are performed well 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:
o Divide the relative population of M in R by R\'s total relative population. o Multiply the result by the total number of captures the trainer intends per region. o 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 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.
o The names and elements of each monster in the monster index.
  • The region atlas.
o The names and monster lists of each region.
  • A list of trainers.
o The names and itineraries of each trainer. o The region list of each itinerary.
typedef struct monster {
typedef struct region {
char *name;
char *name;
char *element;
int monster_cnt;
int population;
int total_population; monster **monsters;
} monster;



} region;
typedef struct itinerary
typedef struct trainer
{
{
int region_cnt;
char *name;
region **regions;
itinerary *visits;
int captures;



} trainer;
} 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.
Example Input and Output Example Input 8 //this represents number of different monsters StAugustine Grass 12 Zoysia Grass 8 WholeWheat Bread 6 MultiGrain Bread 10 Rye Bread 10 Cinnamon Spice 5 Pepper Spice 10 Pumpkin Spice 30 3 //this represents number of regions Rome 4 //this represents number of different monsters in that region StAugustine Zoysia WholeWheat Pepper Helve 5 //this represents number of different monsters in that region StAugustine WholeWheat MultiGrain Rye Cinnamon Aria 5 //this represents number of different monsters in that region Zoysia MultiGrain Cinnamon Pepper Pumpkin 3 //this represents number of trainers Alice 5 //this represents number of captures 2 //this represents number of different regions this training is visiting Rome Aria Bob 4 //this represents number of captures 3 //this represents number of different regions this training is visiting Rome Helve Aria Carol 10 //this represents number of captures 1 //this represents number of different regions this training is visiting Aria Example Output (see the mapping example discussed in the next page to understand how this output is generated) Alice Rome 2-StAugustine 1-Zoysia 1-WholeWheat 1-Pepper Aria 1-Zoysia 1-MultiGrain 1-Pepper 2-Pumpkin Bob Rome 1-StAugustine 1-Zoysia 1-WholeWheat 1-Pepper Helve 1-StAugustine 1-WholeWheat 1-MultiGrain 1-Rye Aria 1-Zoysia 1-MultiGrain 1-Pepper 2-Pumpkin Carol Aria 1-Zoysia 2-MultiGrain 1-Cinnamon 2-Pepper 5-Pumpkin Mapping Example Heres the table of how each individual trainers results are computed. It also shows how rounding issues can lead to trainers capturing more monsters than they intend!
Rome 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

Helve 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

Aria Raw Divided Alice Round Bob Round Carol Round
Coefficient 1.00 63.00 5.00
4.00
10.00
Zoysia 8.00 0.13 0.63 1.00 0.51 1.00 1.27 1.00
MultiGrain 10.00 0.16 0.79 1.00 0.63 1.00 1.59 2.00
Cinnamon 5.00 0.08 0.40 0.00 0.32 0.00 0.79 1.00
Pepper 10.00 0.16 0.79 1.00 0.63 1.00 1.59 2.00
Pumpkin 30.00 0.48 2.38 2.00 1.90 2.00 4.76 5.00
Total 63.00 1.00 5.00 5.00 4.00 5.00 10.00 11.00
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:
o monster* makeMonster(char *name, char *element, int population): This function returns a dynamically allocated monster filled with the provided parameters o 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. o 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 regions 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. o 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 trainers 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. o 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. o 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 o You must #include \"leak_detector_c.h\" in your code, and
o 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 dont 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%
Code compiling and testing in terminal: //if you use math.h library, use the -lm option with the gcc command. Also, note that scanf function returns a value depending on the number of inputs. If you do not use the returned value of the scanf, gcc command may show warning to all of the scanf. In that case you can use -Wno-unused-result option with the gcc command to ignore those warning. So the command for compiling your code would be: # gcc main.c leak_detector_c.c -Wno-unused-result -lm //the following command will execute your code if it was succuessfully compiled in the above step. It also takes input from the in1.txt file and produce the output into myout1.txt file # ./a.out < in1.txt > myout1.txt //To compare your myout1.txt file with the sample out1.txt #diff myout1.txt sample_out1.txt //the result will be blank if there is no difference between the files # diff -c myout1.txt sample_out1.txt //this command will show ! symbol to the unmatched lines. #diff -y myout1.txt sample_out1.txt //very useful to compare the files side by side to see where exactly it is not matching Some hints: - Note: Before starting this assignment, it is assumed that you have reviewed all the DMA examples available in the notes/recording/lecture, exercises, and labs. If you have skipped anyone of them, go back and review them before working on the assignment. - The core concepts of the example of dynamically allocating array of structure pointer, dynamically allocating array of strings, and the Lab2 code would be very useful before starting this assignment. - Start the assignment as soon as possible - Break it down by drawing and designing, - Write each load function and test whether your data is loaded properly - Then gradually implement functions one by one and test your code gradually. - Do not wait till the end to test your code. - Do not hesitate to take help during all of our office hours. Good Luck!

 


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

Entrepreneurship

Authors: Andrew Zacharakis, William D Bygrave

5th Edition

1119563097, 9781119563099

More Books

Students also viewed these Programming questions