Skip to content
Physics and Astronomy
Home Our Teaching Resources C programming assess3-2017.html
Back to top

Assignment 3 (2017)

This assessment forms 13% of your module mark.


  1. To use simple dynamic memory allocation to allocate some structures.
  2. To write a function that accepts a pointer to a structure.
  3. 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 expectations document 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.

The Shape structure

  1. 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.
  2. Now create a structure definition, say "Shape" that stores:
    1. Whether the shape is an ellipse, a triangle or a rectangle (using the named constants you have just created).
    2. 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.)
    3. 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.
  3. 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 previous code.

    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.

Multiple structures

You may do this either by using a dynamically-allocated 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, then:

  • Have your program ask the user how many shapes they want, then allocate this number of shapes, reading the details from the keyboard.

    NB: any "sanity checking" of the input should be done at this stage.

  • Write a function that calls your previous "area" function within a loop and returns the total area of all the shapes. Call it from within main(). If you have chosen to use a dynamically-allocated array you should notice that the "area" function must accept a pointer to a single structure and must be called once for each shape.

To hand in

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 program:

#include <stdio.h>
etc., etc.
  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 similar runs.

To copy and paste from the output terminal

The Windows output console has an "unusual" copy and paste mechanism:

  1. Right-click in the console window and select "Select All".
  2. Press <Return>.

You can now paste into Code::Blocks using Control-V


Validate   Link-check © Copyright & disclaimer Privacy & cookies Share
Back to top