Assignment 3 (2017)
This assessment forms 13% of your module mark.
- To use simple dynamic memory allocation to allocate some structures.
- To write a function that accepts a pointer to a structure.
- To loop over a collection of dynamically-allocated structures.
Remember: you will make the best progress by doing the tasks in
stages, making sure each stage works correctly before starting the
next one. Ask me for help if you get stuck.
Be sure to read the
which will tell you how we will assess your work..
We are using a structure to store information about a shape,
including its dimensions, what type of shape it is (triangle,
ellipse or rectangle) and its area; and writing a function to calculate
the area of such a shape.
- Use #define to create a set of three named integer constants to distinguish
between the three possibilities of a shape being either an ellipse, a
triangle or a rectangle.
- NB Once you have defined names for these constants you must
use those names throughout the rest of the program and never use
the actual values.
- Now create a structure definition, say "Shape" that stores:
- Whether the shape is an ellipse, a
triangle or a rectangle (using the named constants you have just created).
- Its dimensions, ie the lengths of the sides or axes as appropriate.
(For the rectangle it is enough to store the lengths of just two sides.)
- Its area.
- You may use an array of fixed size within
the structure declaration and there is no need to store any
other information such as position, orientation, etc.
- You will end up with a single structure definition that has three
pieces of information: what the shape is (an int), its
dimensions (an array) and its area.
Do not try to create three different structure
definitions, one for each shape.
- Write a function that takes a pointer to a structure and prints
out its type and area. It should also store the value of the area in
the structure and return the value of the area to the calling
program. For the triangle we are expecting you to reuse your
NB: there is no need to "sanity check" the data inside
this function as you may assume this has already been done.
Test this using a dynamically allocated structure, i.e.
allocate the structure within main() and pass the pointer
to the area function.
You may do this either by using a
array of structures or a
linked list of
structures. Note: if you use an array
it must be dyanamically allocated
using one of the malloc() family of functions.
Revise the appropriate lecture and your exercise from last week,
Upload one single C file.
Hand-in guidance (for all assignments)
For all assessments We require one ".c"
text file for each task, uploaded to ELE. Do not upload a PDF,
word-processor file etc.
For each task you should hand in the
program and some output pasted output into a comment at the end of
Your code here
Some output here
The output should be fairly brief but enough to show that your code
works. Basically we are looking for the minumum amount necessary to
show your program worked for each separate class of the problem,
including error checking where appropriate, rather than several
To copy and paste from the output terminal
The Windows output console has an "unusual" copy and paste mechanism:
- Right-click in the console window and select "Select All".
- Press <Return>.
You can now paste into Code::Blocks using Control-V