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 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/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does...
(Write/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. Integers are separated by spaces in the file. Read data back from the file and display the data in increasing order. After writing the file to disk, the input file should be read into an array, sorted using the static Arrays.sort() method from the Java API and then displayed in the...
use python 1. Write a program that a. defines a list of countries that are members...
use python 1. Write a program that a. defines a list of countries that are members of BRICS (Brazil, Russia, India, China, Sri Lanka) b. Check whether a country is a member of BRICS or not Program run Enter the name of country: Pakistan Pakistan is not a member of BRICS Enter the name of country : India India is a member of BRICS 2. Write a program to create a list of numbers in the range of 1 to...
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
Search for Labetalol information: Generic Name, Brand Name, Type of drug, Pregnancy Category, Therapeutic action, ....
Search for Labetalol 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