Question

In: Computer Science

Program C You are given some data from an animal shelter, listing animals that they currently...

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

Solutions

Expert Solution

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!!!!!!!!----------


Related Solutions

Alejandro wants to adopt a puppy from an animal shelter. At the shelter, he finds eight...
Alejandro wants to adopt a puppy from an animal shelter. At the shelter, he finds eight puppies that he likes; a male and female puppy from each of the four breeds of beagle, boxer, collie, and Labrador. Alejandro decides to pick the dog randomly Write the sample space for the outcomes, assuming each outcome is equally likely. Find the probability that Alejandro choose the given puppy. A male boxer A male puppy A collie A female Labrador A beagle or...
Alejandro wants to adopt a puppy from an animal shelter. At the shelter, he finds eight...
Alejandro wants to adopt a puppy from an animal shelter. At the shelter, he finds eight puppies that he likes; a male and female puppy from each of the four breeds of beagle, boxer, collie, and Labrador. Alejandro decides to pick the dog randomly Write the sample space for the outcomes, assuming each outcome is equally likely. Find the probability that Alejandro choose the given puppy. A male boxer A male puppy A collie A female Labrador A beagle or...
Jefferson Animal Rescue is a private non-profit clinic and shelter for abandoned domesticated animals, chiefly dogs,...
Jefferson Animal Rescue is a private non-profit clinic and shelter for abandoned domesticated animals, chiefly dogs, and cats. At the end of 2014, the organization had the following account balances: Debits Credits Pledges receivables $3,500 Cash $25,000 Land buildings& equipment $41,000 Supplies inventory $4,000 Accounts Payable $5,800 Accrued wages payables $500 Accumulated depreciation $19,300 Note payable to bank $25,000 Net assets-temporarily restricted: For use in KDAC program $2,500 For purchase of capital assets $7,200 Unrestricted net assets $13,200 Total $73,500...
c++ program You are to use a Heap data structure for this assignment I currently work...
c++ program You are to use a Heap data structure for this assignment I currently work for an investment/insurance company and I’m looking for clients to call, ones with funds.  I need to have a schedule that shows the list of customers to call and the order to be called.  The list of customers names and phone numbers are in the file ‘NamesAndPhoneV2.txt’.  A second file contains a net worth value for each client.  The files are separated for security and protection reasons, but...
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...
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...
The executive director of Mutt Rescue (MR) animal shelter has asked you to prepare an annual...
The executive director of Mutt Rescue (MR) animal shelter has asked you to prepare an annual budget for the coming fiscal year as well as a flexible budget based on a 5 percent increase in the number of dogs taken into MR’s shelter during the year. She has given you the following guidelines. Number of dogs rescued and placed by MR: 800 Average length of stay for a dog in the shelter: 12 days Daily cost of feeding one dog:...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
Animal cloning is a technique to produce many copies of animals from one cell. Thus, it...
Animal cloning is a technique to produce many copies of animals from one cell. Thus, it is saving us time to breed the animal for world consumption. The very idea of animal cloning is interesting but resulted in so many issues. Discuss the issues and give your opinion on this subject.
Your boss has given you some data on a project (Project C). The project will require...
Your boss has given you some data on a project (Project C). The project will require an initial investment of $450,000 which will result in cash flows of $120,000 in the first three years, and $130,000 in the next three years. It is expected that $15,000 of the initial investment will be recoverable at the end of the project. Assuming a cost of capital of 12% calculate the following (you must provide the units): Assuming a cost of capital of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT