Create a user defined record
Use these records to store collections of data
Perform standard algorithms on arrays of records
For our BMI program we were storing 3 values in 3 parallel arrays:
Height
Weight
BMI
We can store these in one record structure where each one will have these 3 pieces of data (or fields).
RECORD BMI:
Height : REAL
Weight: REAL
BMI: REAL
END RECORD
This defines a data structure that has 3 pieces of information. It is important to indicate the type of each field.
But we will need to have an array of these as we will be holding details about numerous people so...
If we need to declare a single BMI variable
DECLARE BMIrecord AS BMI
And then to use the different ‘fields’ we can use the following:
BMIrecord.height = 1.6
BMIrecord.weight = 62.3
BMIrecord.BMI = BMIrecord.weight * (BMIrecord.height ^ 2)
Declares a dataclass called BMI ( not officially a record but is what we will use in Python. This is only available in Python 3.7 and above.
Declares a single BMI Record.
And then assigns the values in the relevant fields and then calculates the BMI.
Displays the height, weight and BMI fields of the record we just calculated.
As we want to store numerous records such as in the table shown:
We will need to declare an array of records to store each of the set of fields. So there would need to be an array of 4 records.
Data that we wish to store.
Python doesn’t require you to declare variables with types explicitly as it is a loosely typed language so in an exam situation if you were asked to store 40 BMI records using a design notation you could use:
DECLARE BMIDetails (39) AS BMI
BMIDetails[0].height = 1.6
BMIDetails[0].weight = 62.3
This stores the following data.
The second record ( element 1) is empty at the minute but is just there to remind us that we have an array of records.
We will now calculate the BMI and store it in the first record:
BMIDetails[0].BMI = BMIDetails[0].weight / (BMIDetails[0].height ^ 2)
We have used the height and weight fields to calculate the BMI for the first record so are now storing all of the data shown below:
Record 0 with BMI calculated
To insert the next record would use:
BMIDetails[1].height = 1.7
BMIDetails[1].weight = 73.3
BMIDetails[1].BMI = BMIDetails[1].weight / (BMIDetails[1].height ^ 2)
Record 1 now has values stored
Declares a dataclass called BMI ( not officially a record but is what we will use in Python. This is only available in Python 3.7 and above.
Declares an array of 40 BMI Records.
Assigns the values to the fields for the first records in element 0 of the myBMI Array and calculates the BMI.
Displays the details of the first record that we created.
It would be much more efficient to use a loop to create and populate an array of records.
In pseudocode it would look like:
DECLARE BMIDetails (39) AS BMI
FOR i = 0 to LENGTH(BMIDetails)
BMIDetails[i].height = GET INPUT FROM USER
BMIDetails[i].weight = GET INPUT FROM USER
BMIDetails[i].BMI = BMIDetails[i].height / (BMIDetails[i].weight ^ 2)
NEXT LOOP
And in Python:
Loops through for the length of the array and uses the loop counter x to place the values at the specified elements in the array.