in C Languge please! NOT in C++! While it is common to use the formulate 7 dog y
ID: 3832489 • Letter: I
Question
in C Languge please! NOT in C++!
While it is common to use the formulate 7 dog years for every 1 human year, this is actually inaccurate because studies show that larger dogs age more quickly and have shorter life spans than smaller dogs. Small dogs age 15 years for the first human year 8 years for the second human year 5 years for the third human year 4 years for each human year above 3 Medium dogs age 14 years for the first human year 9 years for the second human year 7 years for the third human year 5 years for each human year above 3 Large dogs age 12 years for the first human year 9 years for the second human year 8 years for the third human year 7 years for each human year above 3 Your job is to write a program that will create an array of structures that will hold dog information for 6 different dogs. The program will read the information from a file. The information will include the dog's name, how much the dog weighs, and how old the dog is In human years. You will pass the array of structures to a function called calc() that will calculate whether the dog is small (20 pounds or less), medium (21 to 50 pounds) or large (greater than 50 pounds) and based on its weight, determine how old the dog is in dog years. This information will need to be stored in the structure as well. The array of structures is returned to main() and then needs to be passed to a function called sort(), which asks the user if they wish to sort by name or by size. Complete the sort based on the user's answer. Send back the array to main, which then calls the display() function, which will print out the dog's name, the size of the dog (NOT weight, but the words "Small, Medium, or Large"), its age in human years and its age in dog years.Explanation / Answer
#include<stdio.h>
struct dog {
char name[20];
int weight
int old;
char size[10];
};
calc(struct dog *data){
struct dog *p;
int i;
p = data;
for (i = 0; i<6; i++){
if (*(p+i).weight <= 20))
*(p+i).size = "Small";
if (*(p+i).weight >= 21 && *(p+i).weight <= 50)
*(p+i).size = "Medium";
if (*(p+i).weight > 50)
*(p+i).size = "Large";
}
}
sortbyname( struct dog *data)
{
int i, j;
struct dog *p;
struct dog temp;
for (i=0; i<6; ++i) {
p = data;
for (j=0; j<5-i; ++j){
if (*(p + j).name > *(p+j+1).name)){
temp = *(p+j);
*(p+j) = *(p+j+1);
*(p+j+1) = temp;
}
}
}
}
void display(struct dog *data){
struct dog *p;
p = data;
for (i = 0; i<6; i++){
printf("%s %s %s ", *(p+i).name, " ",*(p+i).size);
}
}
sortbysize( struct dog *data)
{
int i, j;
struct dog *p;
struct dog temp;
for (i=0; i<6; ++i) {
p = data;
for (j=0; j<5-i; ++j){
if (*(p + j).size > *(p+j+1).size)){
temp = *(p+j);
*(p+j) = *(p+j+1);
*(p+j+1) = temp;
}
}
}
}
void main(){
FILE *fp;
int weight, years;
char name[10];
struct dog input[6];
int count;
fp = fopen("Input.txt", "r"); // Assuming Input.txt contains dog's data
if (fp != NULL) {
count = 0;
while (fscanf(fp, "%s %d %d", name, &weight, &years) != EOF){
input[count].name = name;
input[count].weight = weight;
input[count].old = years;
count++;
}
}
else {
printf("File does not exist ");
return;
}
calc(&input[0]);
printf("1. Sort by name ");
printf("2. Sort by size ");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
sortbyname(&input[0]);
break;
case 1:
sortbysize(&input[0]);
break;
}
display(&input[0]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.