Pointers to Structs Assignment

For this assignment you will implement and test a function which calculates the area and perimeter of a rectangle. The rectangle is represented by a struct which contains 2 points, which are also structs. The area and perimeter of the rectangle will also be stored in a struct.

Often when structs are passed to functions, a pointer is passed to save on copying. Similarly, often instead of returning a struct a function modifies a struct through a pointer in order to “return” the result.

Here are the structs you will use in this assignment, which are already defined in rectangle.h:

struct point {
    int x;
    int y;
};

struct rectangle {
    struct point p1;
    struct point p2;
};

struct rectangle_properties {
    int area;
    int perimeter;
};

The p1 and p2 members of struct rectangle represent two points which are diagonally opposite one another in a rectangle. When we looked at this example before, we said that the first point was the upper left point and the second was the lower right. In the struct here, the two points could be any combination of diagonally opposite points (e.g. p1 could be the upper right and p2 the lower left, or vice versa).

The get_rectangle_properties() function will take a pointer to a struct rectangle and a pointer to a struct rectangle_properties as parameters, calculate the area and perimeter of the rectangle pointed to by the struct rectangle pointer, and store the results in the struct pointed to by the struct rectangle_properties pointer.

Here is the prototype:

/**
 * Given a pointer to a rectangle, calculate the area and perimeter of the
 * rectangle and store the results in the struct pointed to by props.
 *
 * Parameters:
 *   rect - pointer to a rectangle
 *   props - pointer to the struct in which to store the results
 */
void get_rectangle_properties(const struct rectangle *rect,
                              struct rectangle_properties *props);

Note that rect is a const struct rectangle *. It is const so that the rectangle remains unchanged. props cannot be const because we need to change the properties.

The library stdlib.h contains a function named abs(). When passed an integer, abs() will return the absolute value of the integer. This makes calculating the width and height easier, since you do not need to check the relative locations of the points.

Submission

Push your submission to git-keeper. You will be graded on correctness and style. The correctness of your function is automatically tested, but you must also have code in main() which is used to test your function. This code will be graded manually.

 


程序代写代做C/C++/JAVA/安卓/PYTHON/留学生/PHP/APP开发/MATLAB


本网站支持淘宝 支付宝 微信支付  paypal等等交易。如果不放心可以用淘宝交易!

E-mail: [email protected]  微信:dmxyzl003 工作时间:无休息工作日-早上8点到凌晨3点


如果您用的手机请先保存二维码到手机,识别图中二维码。如果用电脑,直接掏出手机果断扫描。

 

发表评论