In: Computer Science
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 all the animal information to the screen. You will upload your C program as one file.
C program to collect animal details
Steps:
Source code:
#include <stdio.h>
struct student {
char firstName[50];
int num;
int age;
} s[10];
int main() {
int i,x;
//int x;
printf("Enter the number of animal details you wish to collect :");
scanf("%d",&x);
// storing information
for (i = 0; i < x; ++i) {
s[i].num = i + 1;
printf("\n Number : %d,\n", s[i].num);
printf("Enter animal name: ");
scanf("%s", s[i].firstName);
printf("Enter the age : ");
scanf("%d", &s[i].age);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < x; ++i) {
printf("\nNumber : %d\n", i + 1);
printf("Animal name: ");
puts(s[i].firstName);
printf("Age : %d", s[i].age);
printf("\n");
}
return 0;
}
The output