structures and files Dog year in C languge programing human year, this is acuall
ID: 3832443 • Letter: S
Question
structures and files Dog year in C languge programing
human year, this is acually inaccurate
in C programing please!!!!!! NOT in C++!
CIS 170 Structures and Files Dog Years While it is common to use the formulate years for every 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 calculate whether the dog issmall (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
create the functions calc and sort using the code..
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
struct dog{
char name[20];
int weight;
int humanage;
int dogage;
char *size;
};
int main()
{
struct dog dogarray[10];
int count=0;
FILE *f;
char buffer[255];
char c;
f=fopen("test.txt","rt");
while(fgets(buffer, 255, (FILE*) f)) {
struct dog d1;
char *name;
int weight;
int humanage;
char *p;
p = strtok (buffer," ");
int i=0;
while (p != NULL)
{
if(i==0){
name=p;
}else if(i==1){
weight=atoi(p);
}else if(i==2){
humanage=atoi(p);
}
i++;
p = strtok (NULL, " ,");
}
strcpy(d1.name,name);
d1.weight=weight;
d1.humanage=humanage;
dogarray[count++]=d1;
}
char str[128];
char *ptr;
fclose(f);
int t;
for( t=0;t<count;t++){
if(dogarray[t].weight<=20){
int hage=dogarray[t].humanage;
int dage;
if(hage==1){
dage=15;
}else if(hage==2){
dage=23;
}else if(hage==3){
dage=28;
}else if(hage>3){
dage=28+(hage-3)*4;
}
dogarray[t].dogage=dage;
dogarray[t].size="small";
}else if(dogarray[t].weight>20 && dogarray[t].weight<=50){
int hage=dogarray[t].humanage;
int dage;
if(hage==1){
dage=14;
}else if(hage==2){
dage=23;
}else if(hage==3){
dage=30;
}else if(hage>3){
dage=30+(hage-3)*5;
}
dogarray[t].dogage=dage;
dogarray[t].size="medium";
}else if(dogarray[t].weight>50){
int hage=dogarray[t].humanage;
int dage;
if(hage==1){
dage=12;
}else if(hage==2){
dage=21;
}else if(hage==3){
dage=29;
}else if(hage>3){
dage=29+(hage-3)*7;
}
dogarray[t].dogage=dage;
dogarray[t].size="large";
}
}
printf("dog array : ");
for(t=0;t<count;t++){
printf("%s %s %d %d ",dogarray[t].name,dogarray[t].size,dogarray[t].humanage,dogarray[t].dogage);
}
printf("how you want to sort?");
char sort[10];
scanf("%s",&sort);
if(strcmp(sort,"name")==0){
int i, j;
struct dog temp;
for (i = 0; i < count - 1; i++)
{
for (j = 0; j < (count - 1-i); j++)
{
if (strcmp(dogarray[j].name,dogarray[j + 1].name)>0)
{
temp = dogarray[j];
dogarray[j] = dogarray[j + 1];
dogarray[j + 1] = temp;
}
}
}
}else if(strcmp(sort,"size")==0){
int i, j;
struct dog temp;
for (i = 0; i < count - 1; i++)
{
for (j = 0; j < (count - 1-i); j++)
{
if (strcmp(dogarray[j].size,dogarray[j + 1].size)<0)
{
temp = dogarray[j];
dogarray[j] = dogarray[j + 1];
dogarray[j + 1] = temp;
}
}
}
}
printf(" dog array after sorting: ");
for(t=0;t<count;t++){
printf("%s %s %d %d ",dogarray[t].name,dogarray[t].size,dogarray[t].humanage,dogarray[t].dogage);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.