Question

In: Computer Science

Write a program that defines an animal data type, with an animal name, age, and category...

Write a program that defines an animal data type, with an animal name, age, and category (cat, dog, etc.), as well as an animal array type that stores an array of animal pointers. (ex.

structType *arr[size];) Your program will prompt the user to enter the data for as many animals as they wish. It will initialize a dynamically allocated animal structure for each animal, and it will store each animal in an instance of the animal array structure. Your program will then print all the animal information to the screen. You will upload your C program as one file.

Solutions

Expert Solution

Source code of the program and its working are given below.Comments are included for better understanding the code.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.

Working of the program

  • A structure animal is defined with required fields
  • An array of animal pointers is created based on user enterd size
  • for loop is used to enter information of n animals
  • malloc() function is used to dynamically allocate memory for animal instance and it returns a pointer of type animal.
  • These pointer values(addresses) are stored in the array.
  • animal data are read using array of pointers
  • Finally program prints animal information using a for loop

Source code

#include<stdio.h>
//including stdlib.h for using malloc() function
#include<stdlib.h>
//defining structure animal
struct animal
{
   char name[20];
   int age;
   char category[20];
};
int main()
{
   //declaring integer variables i and n
   int n,i;
   //prompt user to enter number of animals
   printf("Enter the number of animals: ");
   //reading number of animals
   scanf("%d",&n);
   //creating array of animal pointers
   struct animal *arr[n];
   //for loop to read information of n animals
   for(i=0;i<n;i++)
   {
       //allocating memory for structure animal and stores its base address to array of pointers
       arr[i]=(struct animal *)malloc(sizeof(struct animal));
       //prompt user to enter animal information
       printf("\nEnter animal %d information:",i+1);
       printf("\nAnimal name: ");
       //reading into structure animal using pointer
       scanf("%s",&arr[i]->name);
       printf("Animal age: ");
       //reading into structure animal using pointer
       scanf("%d",&arr[i]->age);
       printf("Animal category: ");
       //reading into structure animal using pointer
       scanf("%s",&arr[i]->category);
   }
   printf("\n****Animal information:***");
   //printing animal information using for loop
   for(i=0;i<n;i++)
   {
       printf("\n\nAnimal %d information:",i+1);
       printf("\nName: %s",arr[i]->name);
       printf("\nAge: %d",arr[i]->age);
       printf("\nCategory: %s",arr[i]->category);
   }  
   return 0;      
}

Screen shot of the program

Screen shot of the output


Related Solutions

Write a program that defines an animal data type, with an animal name, age, and category...
Write a program that defines an animal data type, with an animal name, age, and category (cat, dog, etc.), as well as an animal array type that stores an array of animal pointers. Your program will prompt the user to enter the data for as many animals as they wish. It will initialize a dynamically allocated animal structure for each animal, and it will store each animal in an instance of the animal array structure. Your program will then print...
Part A In PyCharm, write a program that prompts the user for their name and age....
Part A In PyCharm, write a program that prompts the user for their name and age. Your program should then tell the user the year they were born. Here is a sample execution of the program with the user input in bold: What is your name? Amanda How old are you? 15 Hello Amanda! You were born in 2005. Write the program. Format your code using best practices. Refer to the zyBooks style guide, if needed, to use proper naming...
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
Write a program to store record of 15 Facebook users using Dictionary data type. Actual name...
Write a program to store record of 15 Facebook users using Dictionary data type. Actual name record should hold username, number of friends, place, date of birth and the date their account was created. https://realpython.com/python-lists-tuples/ https://realpython.com/python-dicts/
Write a program that: (a) Reads name and age of 10 different persons (b) Stores this...
Write a program that: (a) Reads name and age of 10 different persons (b) Stores this information in a dictionary (remember, dictionary contains keys and values) (c) Sorts your dictionary based on keys (d) Sorts your dictionary based on values (e) Displays the original and both sorted dictionaries (step c and d) (f) You search for a specific person and the program displays their name and age on the screen. If the person’s information is not in the dictionary, the...
Write a java program which asks the user to enter name and age and calls the...
Write a java program which asks the user to enter name and age and calls the following methods: printName(): Takes name as parameter and prints it 20 times using a while loop. printAge(): Takes age as parameter and prints all the numbers from 1 up to age. Write a java program that will print first 10 multiples of 3 in a single line.
DO THIS IN C#. Design and implement a program (name it MinMaxAvg) that defines three methods...
DO THIS IN C#. Design and implement a program (name it MinMaxAvg) that defines three methods as follows: Method max (int x, int y, int z) returns the maximum value of three integer values. Method min (int X, int y, int z) returns the minimum value of three integer values. Method average (int x, int y, int z) returns the average of three integer values. In the main method, test all three methods with different input value read from the...
Search for Methylprednisolone information: Generic Name, Brand Name, Type of drug, Pregnancy Category, Therapeutic action, ....
Search for Methylprednisolone information: Generic Name, Brand Name, Type of drug, Pregnancy Category, Therapeutic action, . Indications, Contraindications,. Dose / Adult and Pediatric, Adverse effects, Interactions, Nursing considerations in implementation and Patient Education
Search for Dobutamine information: Generic Name, Brand Name, Type of drug, Pregnancy Category, Therapeutic action, ....
Search for Dobutamine information: Generic Name, Brand Name, Type of drug, Pregnancy Category, Therapeutic action, . Indications, Contraindications,. Dose / Adult and Pediatric, Adverse effects, Interactions, Nursing considerations in implementation and Patient Education
Search for Glucagon information: Generic Name, Brand Name, Type of drug, Pregnancy Category, Therapeutic action, ....
Search for Glucagon information: Generic Name, Brand Name, Type of drug, Pregnancy Category, Therapeutic action, . Indications, Contraindications,. Dose / Adult and Pediatric, Adverse effects, Interactions, Nursing considerations in implementation and Patient Education
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT