CROP STRUCTURE
Crop structure
Jack is an application software developer recently he has been assigned to a new project for crop management by the government. Actually, his task is to develop an app which will maintain the crop details like required rainfall, the temperature suitable for the growth of the plant, sow date, and harvest date. The Government would need to plan the budget for crops during the start of the financial year, for which required water quantity, pesticide, infrastructure to maintain temperature all are factors that affect harvest date of a crop. So help Jack in building filters by temperature and rainfall and sort the crops by sow date, harvest date. This will help the government to plan budget according to the sow and harvest date.
Create a structure called Date.
struct Date{
int dd;
int mm;
int yyyy;
};
Create a structure called Crop.
struct Crop{
char name[30];
float rainfall;
int temperature;
Date sowDate;
Date harvestDate;
};
Get the details from the user.
Assume that no two details are same.
Menu:
1) Crop that needs the highest rainfall
2) Crop that needs the highest temperature
3) Display the crop sorted in ascending order of the sowDate
4) Display the crop sorted in ascending order of the harvestDate
Input and Output Format:
Refer sample input and output for formatting specifications.
All float values are displayed correctly to 2 decimal places.
All text in bold corresponds to the input and the rest corresponds to output.
Sample Input And Output:
Enter the number of Crops
2
Enter the details of Crops 1
Enter name
Rice
Enter rainfall
15
Enter temperature
23
Enter sowDate
25 7 2016
Enter harvestDate
30 11 2016
Enter the details of Crops 2
Enter name
Wheat
Enter rainfall
7
Enter temperature
29
Enter sowDate
24 7 2016
Enter harvestDate
31 11 2016
Menu
1)Crop that needs the highest rainfall
2)Crop that needs the highest temperature
3)Display the crop sorted in ascending order of the sowDate
4)Display the crop sorted in ascending order of the harvestDate
Enter your Choice
1
Crop that needs the highest rain fall is Rice
Menu
1)Crop that needs the highest rainfall
2)Crop that needs the highest temperature
3)Display the crop sorted in ascending order of the sowDate
4)Display the crop sorted in ascending order of the harvestDate
Enter your Choice
2
Crop that needs the highest temperature is Wheat
Menu
1)Crop that needs the highest rainfall
2)Crop that needs the highest temperature
3)Display the crop sorted in ascending order of the sowDate
4)Display the crop sorted in ascending order of the harvestDate
Enter your Choice
3
Wheat
7.00
29
24 7 2016
31 11 2016
Rice
15.00
23
25 7 2016
30 11 2016
Menu
1)Crop that needs the highest rainfall
2)Crop that needs the highest temperature
3)Display the crop sorted in ascending order of the sowDate
4)Display the crop sorted in ascending order of the harvestDate
Enter your Choice
4
Rice
15.00
23
25 7 2016
30 11 2016
Wheat
7.00
29
24 7 2016
31 11 2016
Menu
1)Crop that needs the highest rainfall
2)Crop that needs the highest temperature
3)Display the crop sorted in ascending order of the sowDate
4)Display the crop sorted in ascending order of the harvestDate
Enter your Choice
5
EXIT
#include
#include
#include
struct Crop
{
char name[30];
float rainfall;
int temperature;
struct Date
{
int dd;
int mm;
int yyyy;
}sowDate,harvestDate;
}c[30];
int n,ch,i,j,temp,sdtemp,smtemp,sytemp,hdtemp,hmtemp,hytemp;
float ftemp;
char ctemp[40];
int main()
{
printf("Enter the number of Crops\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the details of Crops %d\n",i);
printf("Enter name\n");
scanf("%s",c[i].name);
printf("Enter rainfall\n");
scanf("%f",&c[i].rainfall);
printf("Enter temperature\n");
scanf("%d",&c[i].temperature);
printf("Enter sowDate\n");
scanf("%d%d%d",&c[i].sowDate.dd,&c[i].sowDate.mm,&c[i].sowDate.yyyy);
printf("Enter harvestDate\n");
scanf("%d%d%d",&c[i].harvestDate.dd,&c[i].harvestDate.mm,&c[i].harvestDate.yyyy);
}
while(1)
{
printf("Menu");
printf("\n1)Crop that needs the highest rainfall\n");
printf("2)Crop that needs the highest temperature\n");
printf("3)Display the crop sorted in ascending order of the sowDate\n");
printf("4)Display the crop sorted in ascending order of the harvestDate\n");
printf("Enter your Choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(c[i].rainfall > c[j].rainfall)
{
ftemp=c[j].rainfall;
c[j].rainfall=c[i].rainfall;
c[i].rainfall=ftemp;
strcpy(ctemp,c[j].name);
strcpy(c[j].name,c[i].name);
strcpy(c[i].name,ctemp);
temp=c[i].temperature;
c[i].temperature=c[j].temperature;
c[j].temperature=temp;
sdtemp=c[i].sowDate.dd;
c[i].sowDate.dd=c[j].sowDate.dd;
c[j].sowDate.dd=sdtemp;
smtemp=c[i].sowDate.mm;
c[i].sowDate.mm=c[j].sowDate.mm;
c[j].sowDate.mm=smtemp;
sytemp=c[i].sowDate.yyyy;
c[i].sowDate.yyyy=c[j].sowDate.yyyy;
c[j].sowDate.yyyy=sytemp;
hdtemp=c[i].harvestDate.dd;
c[i].harvestDate.dd=c[j].harvestDate.dd;
c[j].harvestDate.dd=hdtemp;
hmtemp=c[i].harvestDate.mm;
c[i].harvestDate.mm=c[j].harvestDate.mm;
c[j].harvestDate.mm=hmtemp;
hytemp=c[i].harvestDate.yyyy;
c[i].harvestDate.yyyy=c[j].harvestDate.yyyy;
c[j].harvestDate.yyyy=hytemp;
}
}
}
printf("Crop that needs the highest rain fall is %s",c[n].name);}
break;
case 2:
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(c[i].temperature > c[j].temperature)
{
temp=c[j].temperature;
c[j].temperature=c[i].temperature;
c[i].temperature=temp;
strcpy(ctemp,c[j].name);
strcpy(c[j].name,c[i].name);
strcpy(c[i].name,ctemp);
ftemp=c[i].rainfall;
c[i].rainfall=c[j].rainfall;
c[j].rainfall=ftemp;
sdtemp=c[i].sowDate.dd;
c[i].sowDate.dd=c[j].sowDate.dd;
c[j].sowDate.dd=sdtemp;
smtemp=c[i].sowDate.mm;
c[i].sowDate.mm=c[j].sowDate.mm;
c[j].sowDate.mm=smtemp;
sytemp=c[i].sowDate.yyyy;
c[i].sowDate.yyyy=c[j].sowDate.yyyy;
c[j].sowDate.yyyy=sytemp;
hdtemp=c[i].harvestDate.dd;
c[i].harvestDate.dd=c[j].harvestDate.dd;
c[j].harvestDate.dd=hdtemp;
hmtemp=c[i].harvestDate.mm;
c[i].harvestDate.mm=c[j].harvestDate.mm;
c[j].harvestDate.mm=hmtemp;
hytemp=c[i].harvestDate.yyyy;
c[i].harvestDate.yyyy=c[j].harvestDate.yyyy;
c[j].harvestDate.yyyy=hytemp;
}
}
}
printf("Crop that needs the highest temperature is %s",c[n].name);
break;
case 3:
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if((c[i].sowDate.dd > c[j].sowDate.dd)||(c[i].sowDate.mm > c[j].sowDate.mm)||(c[i].sowDate.yyyy > c[j].sowDate.yyyy))
{
strcpy(ctemp,c[i].name);
strcpy(c[i].name,c[j].name);
strcpy(c[j].name,ctemp);
ftemp=c[i].rainfall;
c[i].rainfall=c[j].rainfall;
c[j].rainfall=ftemp;
temp=c[i].temperature;
c[i].temperature=c[j].temperature;
c[j].temperature=temp;
sdtemp=c[i].sowDate.dd;
c[i].sowDate.dd=c[j].sowDate.dd;
c[j].sowDate.dd=sdtemp;
smtemp=c[i].sowDate.mm;
c[i].sowDate.mm=c[j].sowDate.mm;
c[j].sowDate.mm=smtemp;
sytemp=c[i].sowDate.yyyy;
c[i].sowDate.yyyy=c[j].sowDate.yyyy;
c[j].sowDate.yyyy=sytemp;
hdtemp=c[i].harvestDate.dd;
c[i].harvestDate.dd=c[j].harvestDate.dd;
c[j].harvestDate.dd=hdtemp;
hmtemp=c[i].harvestDate.mm;
c[i].harvestDate.mm=c[j].harvestDate.mm;
c[j].harvestDate.mm=hmtemp;
hytemp=c[i].harvestDate.yyyy;
c[i].harvestDate.yyyy=c[j].harvestDate.yyyy;
c[j].harvestDate.yyyy=hytemp;
}
}
}
for(i=1;i<=n;i++)
{
printf("%s\n",c[i].name);
printf("%0.2f\n",c[i].rainfall);
printf("%d\n",c[i].temperature);
printf("%d %d %d\n",c[i].sowDate.dd,c[i].sowDate.mm,c[i].sowDate.yyyy);
printf("%d %d %d\n",c[i].harvestDate.dd,c[i].harvestDate.mm,c[i].harvestDate.yyyy);
}
break;
case 4:
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if((c[i].harvestDate.dd > c[j].harvestDate.dd)||(c[i].harvestDate.mm > c[j].harvestDate.mm)||(c[i].harvestDate.yyyy > c[j].harvestDate.yyyy))
{
strcpy(ctemp,c[i].name);
strcpy(c[i].name,c[j].name);
strcpy(c[j].name,ctemp);
ftemp=c[i].rainfall;
c[i].rainfall=c[j].rainfall;
c[j].rainfall=ftemp;
temp=c[i].temperature;
c[i].temperature=c[j].temperature;
c[j].temperature=temp;
sdtemp=c[i].sowDate.dd;
c[i].sowDate.dd=c[j].sowDate.dd;
c[j].sowDate.dd=sdtemp;
smtemp=c[i].sowDate.mm;
c[i].sowDate.mm=c[j].sowDate.mm;
c[j].sowDate.mm=smtemp;
sytemp=c[i].sowDate.yyyy;
c[i].sowDate.yyyy=c[j].sowDate.yyyy;
c[j].sowDate.yyyy=sytemp;
hdtemp=c[i].harvestDate.dd;
c[i].harvestDate.dd=c[j].harvestDate.dd;
c[j].harvestDate.dd=hdtemp;
hmtemp=c[i].harvestDate.mm;
c[i].harvestDate.mm=c[j].harvestDate.mm;
c[j].harvestDate.mm=hmtemp;
hytemp=c[i].harvestDate.yyyy;
c[i].harvestDate.yyyy=c[j].harvestDate.yyyy;
c[j].harvestDate.yyyy=hytemp;
}
}
}
for(i=1;i<=n;i++)
{
printf("%s\n",c[i].name);
printf("%0.2f\n",c[i].rainfall);
printf("%d\n",c[i].temperature);
printf("%d %d %d\n",c[i].sowDate.dd,c[i].sowDate.mm,c[i].sowDate.yyyy);
printf("%d %d %d\n",c[i].harvestDate.dd,c[i].harvestDate.mm,c[i].harvestDate.yyyy);
}
break;
default:
printf("EXIT\n");
exit(0);
}
}
return 0;
}
Comments
Post a Comment