Click here to Skip to main content
15,867,878 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:








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]

// main function
int main()  {
    struct Section *allSections;
   
   
   
    
}

return 0;
}
Posted
Updated 6-Sep-22 9:14am
Comments
Greg Utas 6-Sep-22 14:28pm    
What you have "tried" so far isn't remotely close to a reasonable attempt at this exercise. If this is an assignment that you have been given, you should be able to produce much more than this. If you can't, you have a lot of catching up to do.

Obviously, the first thing to do is to read in a file with commands. The animals can be in the cage or in the stock.The stock is thus to be understood as a section with cages with unlimited size.

The commands are numbered and with their parameters already given. Since neither the number of park sections nor the number of cages are known conclusively, it would be necessary to manage both quantities in chained lists, for example. The park consists of sections, which in turn contain cages. It is recommended to write frequently needed functions as subroutines.

Based on the presented code it could be modeled like this:
C
#define MAXNAMELEN 1000

typedef struct Cagetyp {
	char animal_name[MAXNAMELEN+1];
	unsigned count;
	struct Cagetyp* next;
} Cage;

typedef struct Sectiontyp {
	Cage* cages;
	unsigned count;
	struct Sectiontyp* next;
} Section;

typedef struct {
	Section* sections;
	unsigned numSections;
} Zoo;

Section* AddSection(Zoo *zooPtr)
{
    // allocate memory for section
 	Section* newSection = (Section *)calloc(1, sizeof(Section));
	if (newSection == NULL)
		return NULL;

	// init new section
	newSection->cages = NULL;	// List of Cages is empty
	newSection->count = 0;		// No cages in section

	// put section (at top) in chain
	newSection->next = zooPtr->sections;
	zooPtr->sections = newSection;
	zooPtr->numSections++;
	return newSection;
}

Cage*  AddCage(Section* s, char* animal_name, unsigned count)
{
    // allocate memory for cage
	Cage* newcage = (Cage*)calloc(1, sizeof(Cage));
	if (newcage == NULL)
		return NULL;

	// TODO: init new cage

	// TODO: put cage (at top) in chain

	return newcage;
}

// main function
int main() 
{
	Section *stocksec, *sec1;
	Cage* newcage;

	Zoo zoo = {NULL, 0};

	// create Sections
	stocksec = AddSection(&zoo);	// Section 0
	sec1     = AddSection(&zoo);
	
	// TODO: read input

	// example: 
	char *animal_name1 = "greydog";
	newcage = AddCage(sec1, animal_name1, 3);

	return 0;
}
 
Share this answer
 
v3
Comments
omar geda 6-Sep-22 18:44pm    
Okay, I have tried that, but it didn't compile.
merano99 7-Sep-22 4:37am    
What exactly cannot be compiled?
As usual, the associated includes have to be added and also the AddSection() and AddCage() functions have to be added by yourself, of course. The proposal as a concept should be an essential part of the solution to the problem. The completed source code was compiled without errors and warnings.
Patrice T 7-Sep-22 7:47am    
I guess compiler chocked on "..." as OP did not understood that the code is not complete.
+5
merano99 7-Sep-22 18:41pm    
Thank you for pointing this out. I will use a commented out TODO in the future. Of course, this will not eliminate the problem of the expected own contribution by itself.
omar geda 9-Sep-22 20:01pm    
how would the code look after it's finished? because I'm confused while looking at it.
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Just posting your assignment isn't going to get you anywhere.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
To begin, reread the requirements. An animal can be at most 1000 characters so try this :
C++
#define ANIMAL_SIZE 1000

typedef struct Cage
{
   char animal[ANIMAL_SIZE + 1];
};
I will also show you how to dynamically allocate cages. You can use the function calloc - C++ Reference[^] to do this. Let's say you want cageCount cages. We'll store them in this variable :
C++
Cage * cages;
here's how you can allocate them :
C++
cages = (Cages *)calloc( cageCount, sizeof( Cage ) );
That can represent the cages in one section of the park. You will need to have an array of sections that can be constructed similarly. I will leave that for you to figure out.

You can modify you question (use improve question) and add more code if have more questions.
 
Share this answer
 
Comments
omar geda 6-Sep-22 18:45pm    
okay thank you, do I do that for each struct?
Rick York 6-Sep-22 19:45pm    
For each section there is an array of cages. What I showed you was the array for one section. Next you will need an array of structures for each section. Solution 3 shows how that can be done and similar techniques can be used to allocate those arrays.
merano99 7-Sep-22 4:46am    
You can use Rick's suggestion to write subroutines like AddSection() and AddCage(). And of course you call the functions multiple times, for each section and for each cage.
omar geda 9-Sep-22 22:41pm    
I'm sorry , how would that work? I tried it but it didn't work for me. Do I replace cages with a number? I'm super confused.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900