CMSC 15200 - Summer 2013

Lab 3: Shapes

Preliminaries

Create and svn add a lab3 directory inside your repostiory.

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. You will then collect these functions and types into a separate library.

Part 1 - Defining Structs

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

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).


Part 2 - Functions and Pointers

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

  • Perimeter
  • Area

I.e. you should have functions like rectanglePerimeter, rectangleArea, circlePerimeter, circleArea, ….

You are required to implement this lab to be pointers to shapes rather than shapes themselves.

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

typedef struct rectangle *        Rect;

Please make constuction/make functions for each of your shapes.

Rect mkRect( ... )

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.

Write test cases in your main function for testing your perimeter and area functions.

Naming Conventions

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

Struct definitions are lower-case

struct triangle
{
    ...
}

Pointer typedefs are upper-case

typedef struct triangle *      Triangle

So

  • Triangle is a pointer to a struct triangle
  • Rect is a pointer to a struct rect, etc….

In this naming scheme upper case types are pointers. Lower-case types are not.

Whenever you have a variable with a lower-case type you use dot

struct point p;
p.x = 5

Whenever you have a variable with an upper-case type you use arrow

Point p = malloc(sizeof(struct point));
p->x = 5

Once completed, commit the file to your repository

You have completed the lab! Call me over to show me your lab for credit.


So What's Next?

Conitune working on your HW#4 assignment while you have teaching staff present.