Problem: Given a list of commands determine at particular points what animal is contained in a requested cage.   Input: will begin with a line containing 1 integer, N (1 ≤ N ≤ 100,000), representing the number of park sections. Each park section initially contains 0 cages. Following this line will be list of commands. The commands must be processed in the order they are given. Each command begins with a number and can be 1 of 4 types. The types are listed below ●1 S C ○This command changes the number of cages in some particular section. The section that is modified is the section numbered S (1-indexed). The resulting number of cages in section S is the number C. This could increase or decrease the number of cages. Any animal that was in a cage with ID after C (1-indexed) is sent back to the warehouse. If the section number is invalid, no changes should be made. ●2 S C A ○This command adds an animal to a cage. The section that is modified is the section numbered S (1-indexed). The resulting cage the animal is added to is C (1-indexed), if the cage is empty and the section and cage exists. If an animal is already present in cage C of section S or there is no section S or cage C of section S does not exist, then the animal is NOT added to the cage. The type of animal is A (a string of at most 1000 alphanumeric characters). ●3 S C ○This command requests the animal type of the animal in cage C (1-indexed) of section S. If no animal is present or the section or cage number is invalid, the phrase “No animal found.” (quotes for clarity) should be printed instead. ●4 ○This command requests that the program ends (I guess we finished organizing all the exhibits).     Output: should contain a line for each command of type 3. The line should either contain the animal type if the request was valid or the string “No animal found.”

COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
1st Edition
ISBN:9780357392676
Author:FREUND, Steven
Publisher:FREUND, Steven
Chapter8: Working With Trendlines, Pivottables, Pivotcharts, And Slicers
Section: Chapter Questions
Problem 8EYK
icon
Related questions
Question
I need help writing this code in C using dyanmic memory and structs.
 
Problem: Given a list of commands determine at particular points what animal is contained in a requested cage.
 
Input: will begin with a line containing 1 integer, N (1 ≤ N ≤ 100,000), representing the number of park
sections. Each park section initially contains 0 cages.
Following this line will be list of commands. The commands must be processed in the order they are
given. Each command begins with a number and can be 1 of 4 types. The types are listed below
●1 S C
○This command changes the number of cages in some particular section. The section that
is modified is the section numbered S (1-indexed). The resulting number of cages in
section S is the number C. This could increase or decrease the number of cages. Any
animal that was in a cage with ID after C (1-indexed) is sent back to the warehouse. If the
section number is invalid, no changes should be made.
●2 S C A
○This command adds an animal to a cage. The section that is modified is the section
numbered S (1-indexed). The resulting cage the animal is added to is C (1-indexed), if the
cage is empty and the section and cage exists. If an animal is already present in cage C of
section S or there is no section S or cage C of section S does not exist, then the animal is
NOT added to the cage. The type of animal is A (a string of at most 1000 alphanumeric
characters).
●3 S C
○This command requests the animal type of the animal in cage C (1-indexed) of section S.
If no animal is present or the section or cage number is invalid, the phrase “No animal
found.” (quotes for clarity) should be printed instead.
●4
○This command requests that the program ends (I guess we finished organizing all the
exhibits).
 
 
Output: should contain a line for each command of type 3. The line should either contain the animal type if
the request was valid or the string “No animal found.”
Sample Input
5
1 5 10
2 5 8 cat
dog
2 59
359
358
158
3 5 8
359
4
2
1 1 1
3 1 1
2 1 1 Qawous
3 1 1
4
Sample Output
dog
cat
cat
No animal found.
No animal found.
Qawous
Transcribed Image Text:Sample Input 5 1 5 10 2 5 8 cat dog 2 59 359 358 158 3 5 8 359 4 2 1 1 1 3 1 1 2 1 1 Qawous 3 1 1 4 Sample Output dog cat cat No animal found. No animal found. Qawous
Storage: To avoid creating too much memory, you should use dynamic memory to keep track of both the
cages and the names within each cage. I recommend using a struct for each section (using a struct is 5
points of the rubric). See the following code snippet for my recommendation.
struct Section
{
};
struct Cage * cages;
int numCages;
Your main function you would look like the following
int main()
{
}
struct Section * allSections;
// Read in the number of sections
allSections = allocation size based on the number of sections;
// Handle commands
// Clean up remaining memory
return 0;
Transcribed Image Text:Storage: To avoid creating too much memory, you should use dynamic memory to keep track of both the cages and the names within each cage. I recommend using a struct for each section (using a struct is 5 points of the rubric). See the following code snippet for my recommendation. struct Section { }; struct Cage * cages; int numCages; Your main function you would look like the following int main() { } struct Section * allSections; // Read in the number of sections allSections = allocation size based on the number of sections; // Handle commands // Clean up remaining memory return 0;
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L