CMSC 15200 - Summer 2016

Lab 3: Shapes

Preliminaries

Create and svn add a lab3 directory inside your repository.

You will write all your code for this lab in a file named lab3.c


In this lab, you will define new data types to describe various shapes. You will write a few functions that work on these types.

Part 1 - Defining/Allocating Structs

Consider the following circles. How could we describe them as variables in C?

Struct Definitions

Design new types using struct for the following shapes:

  • Square
  • Rectangle
  • Circle

Your definitions must be general enough to describe shapes of different sizes and centered at different locations.

You will need to create a Point type, which will contain the fields x and y. This will be more complex and difficult (you will have structs within structs) but will be conceptually satisfying (your code will look more like math).

Allocating Structs

Please make convenient typedefs to minimize typing. I.e.

typedef struct rectangle Rect;

Implement helper allocator functions for each of your shapes:

Point* point_alloc( ... ) { ... } 
Rect* rect_alloc( ... ) {... }
Circle* circle_alloc( ... ) { ... }
Square* square_alloc( ... ) { ... }

These functions should take in the necessary inputs to define a shape, malloc enough space for the shape, set the inputs and return a pointer to the shape’s location in memory.


Part 2 - Functions and Pointers

Write functions to compute the following for each of your shape data types

  • Perimeter
  • Area

I.e. you should have functions like rectangle_perimeter, rectangle_area, circle_perimeter, circle_area, ….

Write at least two test cases in your main function for testing your perimeter and area functions. Also make sure to deallocate the pointers once you are done using them.

Naming Conventions

Use the following naming conventions in this lab and further labs/homeworks.

  • Struct definitions are lower-case:
  • struct triangle {
        ...
    };
  • Typedefs are upper-case:
  • typedef struct triangle Triangle;
An important note on typedefs:
  • In previous years, the lab stated to create typedefs that hide the fact that the type is a pointer, e.g.
    typedef struct triangle * Triangle;
  • This is perfectly valid to do in C.
  • In this teacher's opinion, however, this is a BAD IDEA because it hides an important property of the data type from the programmer. The programmer should be able to look at the variable declaration (or function parameter) and know immediately if it is a pointer.
  • Also consider if you had a double pointer to a struct circle, e.g. struct circle ** c. If using the typedef that includes the pointer property, it would be Circle* c instead of Circle** c which can make for additional confusion about how many layers of indirection exist to the actual memory holding the structure.

  • Once completed, commit the file to your repository.

    So What's Next?

    Continue working on your HW#5 assignment while you have teaching staff present.