Skip to content

Preliminary

Arrays contain homogeneous data (i.e., data of the same type). This is typically useful for something like the student results in a classroom since we have the same type. In this case, a student result type.

But how do we record the data for students? It cannot be all double to record their height, weight and CAP since we also need to store their name, which is a string. So what can we use?

Structures

This is where structures are useful. Structures are basically a "glue" to combine multiple values from (possibly) different data types together.

Structures

A bank account typically contains two values:

  1. Account number: int (may also be a string if there are other characters beside digits).
  2. Account balance: double (depending on precision, may also use sign-and-magnitude but no primitive type corresponding to that).

Bank Account

A student result may contain three values:

  1. The student number: int (although NUS matric number has the format of AxxxxxxxZ, the only important component is the digits xxxxxxx).
  2. The student score: double.
  3. The student grade: char (or two characters for the + and - symbol).

Student Result

Note that once a structure is created, it can be a member of another group. This allows us to create very complex structures with nested structures.

Nested Structures

Date

Consider a date with three values:

  1. Day: int.
  2. Month: int.
  3. Year: int.

This date structure can then be used in other structures. For instance, consider a membership card containing an expiry date member which is a date structure.

Membership Card