CSSE 120 -- Intro. to Software Development

Homework 24

  1. Don't forget to refer to the Python vs C comparison document. You may find this helpful as you try to do things in C that you already know how to do in Python. Feel free to suggest things that we might add to this document.
  2. Do the reading in the Kochan C textbook (if you have the text) listed on the schedule page or the material linked from the schedule page for the next session.
  3. (10 points ) Complete the ANGEL quiz over the reading assignment. You'll find the quiz on the course ANGEL page, under Lessons → Homework → Homework 24 → Quiz 24: Pointers and Functions in C
  4. You must do this assignment using the Session24_RectangleStructs project that you checked out from your individual SVN repository in class.

    Be sure to check out that project into your Eclipse C workspace, not your Python workspace.

  5. (35 points) This problem involves defining and using some structures (structs) to model filled rectangles on the Cartesian (x, y) plane. A rectangle is represented by two points, point1 and point2, indicating opposite corners of the rectangle. The figure below illustrates the problem. The labels in the figure correspond to the variable names in the provided test code.

    Intersecting rextangles

    The parts of this problem must be completed in the order given; testing of the later steps depends on your implementation of the earlier steps. Part (b) should be completed in file rectangle.h and the remaining parts of the problem should be completed in rectangle.cThe file main.c should NOT be modified.

    1. (3 pts) Name and programming style
    2. (2 pts) We've provided an empty struct type Rectangle in file rectangle.h. Change this struct to store the necessary information (the points described above).
    3. (2 pts) Now write a function Rectangle makeRect(int x1, int y1, int x2, int y2) in rectangle.c that makes a Rectangle from the given coordinates, where point1 has coordinates (x1, y1) and point2 has coordinates (x2, y2). Any opposite corners may be given; for example r1 in the figure could be generated using any one of the following:

      • r1 = makeRect(-3, 3, 5, -2) ,
      • r1 = makeRect(-3, -2, 5, 3) ,
      • r1 = makeRect(5, -2, -3, 3) , or
      • r1 = makeRect(5, 3, -3, -2).

      The function stub is already included, as is code in main() that tests your function.

    4. (2 pts) Write a function int getLeft(Rectangle r) that returns the x-coordinate of the left-most edge of the given rectangle. For example, getLeft(r1) should return -3. The function stub is already included, as is code in main() that tests your function.
    5. (2 pts) Write a function int getRight(Rectangle r) that returns the x-coordinate of the right-most edge of the given rectangle. For example, getRight(r1) should return 5. The function stub is already included, as is code in main() that tests your function.
    6. (2 pts) Write a function int getTop(Rectangle r) that returns the y-coordinate of the top-most edge of the given rectangle. For example, getTop(r1) should return 3. (Note: y values increase as we move up, like in mathematics.) The function stub is already included, as is code in main() that tests your function.
    7. (2 pts) Write a function int getBottom(Rectangle r) that returns the y-coordinate of the bottom-most edge of the given rectangle. For example, getBottom(r1) should return -2. The function stub is already included, as is code in main() that tests your function.
    8. (12 pts) Write a function int areIntersecting(Rectangle q, Rectangle r) that returns a "true" value if the given rectangles touch and a "false" value if they do not touch. For example, areIntersecting(r1,r3) should return "false", but areIntersecting(r1,r2) and areIntersecting(r2,r3) should both return "true". The function stub is already included, as is code in main() that tests your function.
    9. (8 pts) Write a function Rectangle intersect(Rectangle q, Rectangle r) that returns a new rectangle representing the overlapping area of the two given rectangles. You may assume that the two rectangles do, in fact, overlap. The function stub is already included, as is code in main() that tests your function.

    When your code is working, commit it to your repository.