I am writing a program to calculate a rectangle's area.
Eg.
Enter top left point: 1 1 (User input)
Enter bottom right point: 2 -1 (User input)
Top Left x = 1.000000 y: 1.000000
Bottom Right x = 2.000000 y: -1.000000
Area = 2.000000 (Program output)
But mine is:
Enter top left point: 1 1 (User input)
Enter bottom right point: 2 -1 (User input)
w: 1.000000
h: 2.000000
w*h: 2.000000
Rectangle Area: 1.000000 // This is the problem, it should be printed out as 2.000000 instead.
.
typedef struct {
double x;
double y;
} Point;
typedef struct {
Point topLeft; /* top left point of rectangle */
Point botRight; /* bottom right point of rectangle */
} Rectangle;
Point p;
Rectangle r;
printf("Enter top left point : ");
scanf("%lf",&r.topLeft.x);
scanf("%lf",&r.topLeft.y);
fflush(stdin);
printf("Enter bottom right point : ");
scanf("%lf",&r.botRight.x);
scanf("%lf",&r.botRight.y);
computeArea(&r);
printf("Rectangle Area : %lf",r);
double computeArea(Rectangle *r)
{
double h=0,w=0,z=0;
w=fabs(r->topLeft.x - r->botRight.x);
printf("w : %lf\n",w);
h=fabs(r->botRight.y - r->topLeft.y);
printf("h : %lf\n",h);
z=w*h;
printf("w*h : %lf\n",z);
return (z);
}