Give practice with dynamic memory in C.
Story
You managed to round up all the animals. Now you need to structure your park. You and your employees
need to be able to look up information quickly, so you will write a program to help assist you and your
“assistant to the park managers”. Again the park is composed of multiple sections, and each section will
have multiple cages. You plan on doing the following,
● Changing the number of cages within particular park sections
● Adding and removing animals to different cages within particular sections
● Listing the animal within a particular cage of a particular park section
Problem
Given a list of commands determine at particular points what animal is contained in a requested cage.
Input
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
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 Sample Output
5
1 5 10
2 5 8 cat
2 5 9 dog
3 5 9
3 5 8
1 5 8
3 5 8
3 5 9
4
dog
cat
cat
No animal found.
2
1 1 1
3 1 1
2 1 1 Qawous
3 1 1
4
No animal
How do I do this problem in C programming using structs and memory?
What I have tried:
I have tried this.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int arr[5];
arr[1]
struct Cage {
char animal[100000];
}
struct Section {
struct Cage * cages;
int numCages;
};
"dog"
allSection[5].cages[0]
int main() {
struct Section *allSections;
}
return 0;
}