In: Computer Science
I have a problem with my code. It does not run. Please can someone check the code and tell me where I went wrong?
This is the question:
Program Specification: Write a C program that takes the length and width of a rectangular yard, and the length and width of a rectangular house (that must be completely contained in the yard specified) as input values. Assuming that the yard has grass growing every where that the house is not covering, and you own a lawn mower can mow one square foot per second, on average. Compute the time (in minutes) needed to mow the yard.
Requirements: 1. Use conditional logic to display an error message (instead of erroneous output) only if neither the specified house nor the same house rotated 90 degrees does not fit on the specified yard. 2. Otherwise - display all of the user’s inputs and the computed result in a reasonable report like format.
This is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float Length_y;
float Width_y;
float Length_h;
float Width_h;
float Area_y;
float Area_h;
float Area_G;
float Time;
printf("Enter the length of the yard: \n");
scanf("%f", &Length_y);
printf("Enter the width of the yard: \n");
scanf("%f", &Width_y);
printf("Enter the length of the house: \n");
scanf("%f", &Length_h);
printf("Enter the width of the house: \n");
scanf("%f", &Width_h);
if
((Length_h>=Length_y)||(Width_h>=Width_y))
{
Length_h= Width_h;
Width_h= Length_h;
Area_y= Length_y*Width_y;
Area_h= Length_h*Width_h;
Area_G= Area_y-Area_h;
Time= Area_G/60;
printf("Length of the yard is %f
\n", Length_y );
printf("Width of the yard is %f
\n", Width_y );
printf("Length of the house is %f
\n", Length_h );
printf("Width of the house is %f
\n", Width_h);
printf("Area of the yard is %f \n",
Area_y);
printf("Area of the house is %f
\n", Area_h);
printf("Area with grass is %f \n",
Area_G);
printf("Time to mow lawn is %f \n",
Time);
}
else
{
printf("House does not fit in
yard.\n");
}
return 0;
}
Error code with comments:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float Length_y;
float Width_y;
float Length_h;
float Width_h;
float Area_y;
float Area_h;
float Area_G;
float Time;
printf("Enter the length of the yard: \n");
scanf("%f", &Length_y);
printf("Enter the width of the yard: \n");
scanf("%f", &Width_y);
printf("Enter the length of the house: \n");
scanf("%f", &Length_h);
printf("Enter the width of the house: \n");
scanf("%f", &Width_h);
if ((Length_h>=Length_y)||(Width_h>=Width_y))
// The bodies of if and else are reversed
{
Length_h= Width_h; // if a is
enterd in length_h and b in width_h then this step makes
Width_h= Length_h; // width_h=
length_h = b. these two steps are not required
Area_y= Length_y*Width_y;
Area_h= Length_h*Width_h;
Area_G= Area_y-Area_h;
Time= Area_G/60;
printf("Length of the yard is %f
\n", Length_y );
printf("Width of the yard is %f
\n", Width_y );
printf("Length of the house is %f
\n", Length_h );
printf("Width of the house is %f
\n", Width_h);
printf("Area of the yard is %f \n",
Area_y);
printf("Area of the house is %f
\n", Area_h);
printf("Area with grass is %f \n",
Area_G);
printf("Time to mow lawn is %f \n",
Time);
}
else
{
printf("House does not fit in
yard.\n");
}
return 0;
}
Corrected Code ------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
int main()
{
float Length_y;
float Width_y;
float Length_h;
float Width_h;
float Area_y;
float Area_h;
float Area_G;
float Time;
printf("Enter the length of the yard: \n");
scanf("%f", &Length_y);
printf("Enter the width of the yard: \n");
scanf("%f", &Width_y);
printf("Enter the length of the house: \n");
scanf("%f", &Length_h);
printf("Enter the width of the house: \n");
scanf("%f", &Width_h);
if ((Length_h>=Length_y)||(Width_h>=Width_y))
{
printf("House does not fit in yard.\n");
}
else
{
Area_y= Length_y*Width_y;
Area_h= Length_h*Width_h;
Area_G= Area_y-Area_h;
Time= Area_G/60;
printf("Length of the yard is %f \n", Length_y );
printf("Width of the yard is %f \n", Width_y );
printf("Length of the house is %f \n", Length_h );
printf("Width of the house is %f \n", Width_h);
printf("Area of the yard is %f \n", Area_y);
printf("Area of the house is %f \n", Area_h);
printf("Area with grass is %f \n", Area_G);
printf("Time to mow lawn is %f \n", Time);
}
return 0;
}