In: Computer Science
Program C You are given some data from an animal shelter, listing animals that they currently have. They have asked you to write a program to sort the dogs and cats in age in ascending order, respectively, and write them in separate files. Assume the input file has the format of name (one word), species (one word), gender (one word), age (int), weight (double), with each animal on a separate line:
Hercules cat male 3 13.4
Toggle dog female 3 48
Buddy lizard male 2 0.3 ….
Example input/output:
Enter the file name: animals.txt
Output file name: sorted_dogs.txt sorted_cats.txt
1. Name your program animals.c.
2. The output file name should be sorted_dogs.txt and sorted_cats.txt. Assume the input file name is no more than 100 characters.
3. The program should be built around an array of animal structures, with each animal containing information of name, species, gender, age, and weight. Assume that there are no more than 200 items in the file. Assume the name of an animal is no more than 100 characters.
4. Use fscanf and fprintf to read and write data.
5. Your program should include a sorting function so that it sorts the animals in age. You can use any sorting algorithms such as selection sort and insertion sort. void sort_animals(struct animal list[], int n);
6. Output files should be in the format of name gender age weight, with 2 decimal digits for weight.
For example,
Toggle female 3 48.01
Rocky male 5 52.32
SOLUTION-
I have solve the problem in C code with comments and screenshot for
easy understanding :)
CODE-
//c code
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
// structure for animal
struct animal{
char name[100];
char species[100];
char gender[10];
int age;
float weight;
};
// function to sort animal based on age
void sort_animals(struct animal list[], int n){
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (list[j].age < list[min_idx].age)
min_idx = j;
// Swap the found minimum element with the first element
struct animal temp = list[min_idx];
list[min_idx] = list[i];
list[i] = temp;
}
}
int main(){
// count of animal
int count = 0;
// array of animals
struct animal animals[200];
// file pointers
FILE *infile;
FILE *out1, *out2;
// animal to read input from file
struct animal input;
// file name
char fileName[200];
// read file name
printf("Enter file name: ");
scanf("%s", fileName);
// Open animals.txt for reading
infile = fopen (fileName, "r");
if (infile == NULL)
{
fprintf(stderr, "\nError opening file\n");
exit (1);
}
// read file contents till end of file and insert to array
while(fscanf(infile, "%s %s %s %d %f",input.name,input.species,
input.gender, &input.age, &input.weight)!=EOF)
animals[count++] = input;
// close file
fclose (infile);
//sort animals
sort_animals(animals, count);
// open files for writing
out1 = fopen("sorted_dogs.txt", "w");
out2 = fopen("sorted_cats.txt", "w");
int i;
// write to files by comparing species of each animal
for(i=0; i<count; i++){
if(strcmp(animals[i].species, "dog")==0){
fprintf(out1, "%s\t%s\t%d\t%.2f\n",
animals[i].name,animals[i].gender,animals[i].age,animals[i].weight);
}
else if(strcmp(animals[i].species, "cat")==0){
fprintf(out2, "%s\t%s\t%d\t%.2f\n",
animals[i].name,animals[i].gender,animals[i].age,animals[i].weight);
}
}
//close files
fclose(out1);
fclose(out2);
return 0;
}
SCREENSHOT-
OUTPUT
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------