In problem 2 and 3, assume that a rectangle has the following structure: struct
ID: 3554105 • Letter: I
Question
In problem 2 and 3, assume that a rectangle has the following structure:
struct rect
{
double x, y. width, height;
};
2. Write a function to read and assign values to all data members of a rectangle structure. The function should prompt the user for the input values. The output should have the following format:
Rectangle is at location: x.xx x.xx
The width is: x.xx
The height is: x.xx
3. Write a function that takes two rectangle structures as arguments and returns the value 1 if the rectangles have the same location and size; it returns zero otherwise. The function should prompt the user for the input values. The output should include the location, width, and height of the two rectangles. The format is similar to the required output of problem 2. It also should include the result of the comparison. (ie. if the two rectangles have the same location and size.)
*The following problems should be written in C language*
Explanation / Answer
for #2
#include <stdio.h>
#include <string.h>
struct rect
{
double x, y, width, height;
};
struct rect getUserinput()
{
struct rect r;
printf("Enter Rectangle location: ");
scanf("%lf %lf", &r.x, &r.y);
printf("Enter Rectangle width: ");
scanf("%lf", &r.width);
printf("Enter Rectangle height: ");
scanf("%lf", &r.height);
return r;
}
int main()
{
struct rect record;
record = getUserinput();
printf(" Rectangle is at location: %lf %f ", record.x, record.y);
printf(" The width is: %f ", record.width);
printf(" The height is %f ", record.height);
return 0;
}
for #3
#include <stdio.h>
#include <string.h>
struct rect
{
double x, y, width, height;
};
struct rect getUserinput(int num)
{
struct rect r;
printf("Enter Rectangle location %d: ", num);
scanf("%lf %lf", &r.x, &r.y);
printf("Enter Rectangle %d width: ", num);
scanf("%lf", &r.width);
printf("Enter Rectangle %d height: ", num);
scanf("%lf", &r.height);
return r;
}
int compareRects(struct rect r1, struct rect r2)
{
if ((r2.x == r1.x) && (r2.y == r1.y) && (r2.width == r1.width) && (r2.height == r1.height))
return 1;
else
return 0;
}
int main()
{
struct rect record;
struct rect record1;
int cmp;
record = getUserinput(1);
record1 = getUserinput(2);
printf(" Rectangle1 is at location: %lf %f ", record.x, record.y);
printf(" The width is: %f ", record.width);
printf(" The height is %f ", record.height);
printf(" Rectangle2 is at location: %lf %f ", record1.x, record1.y);
printf(" The width is: %f ", record1.width);
printf(" The height is %f ", record1.height);
cmp = compareRects(record, record1);
if (cmp == 1)
printf("Rectangle 1 is the same as Rectangle 2");
else
printf("Rectangle 1 is NOT the same as Rectangle 2");
return 0;
}