With Task 1 now complete, it is now time to tackle task 2.
TASK 2 – Calculate the statistics.
Using your recorded data from TASK 1, calculate and display the total weekly volume of milk for the herd to the nearest whole litre. Calculate and display the average yield per cow in a week to the nearest whole litre
Task Break down
Key Points
Required Information The total milk from all cows over 7 days to 0 Decimal places - Variable of integer type to store total milk The Average yield per cow over 7 days per cow - Variable of Integer type to store average yield
Assumptions We are to assume that task 1 is complete and all data has been collected
Program Flow
The following program flow steps give a brief outline of the key steps involved in task 2.
Step 1 Create new variables to store required information First we will need a new variable to store the total milk yield once they have all been added up - Name = TotalMilk Next we need to declare a new variable that will store the average milk yield - Name = AvgMilk
Step 2 Loop through stored cow yields ( TotalMilkYield [] ) and find the total An initial For loop needs to be set up, this will be set up to loop 7 times - once for each day Within the days for loop, we need another for loop that will repeat for as many times as we have cows in the herd. Inside the inner loop we will look at the current value of the TotalMilkYield [] array and add it to the TotalMilk Variable
Step 3 Calculate the average milk yield The value stored in the TotalMilk array is divided by the number of times that cows were milked. This value could be gained in a number of ways e.g.
AvgMilk = TotalMilk / len(TotalCowYield[]) or AvgMilk = TotalMilk / ( (2 * CowHerdSize) * 7) or AvgMilk = TotalMilk / Count2 (Count2 explained in the code section below)
Pseudocode and Python code
The sections below will walk you through in more technical detail the coding process to solve task 1. Both Pseudocode and Python code examples will be provided:
Step 1
In order to calculate the total and average milk yields we are first going to have to declare some new variables to store critical information. For task 2 we need 3 new variables:
TotalMilk - To store the total amount of milk AvgMilk - To store the average milk amount Count2 - To help loop through the TotalCowYield [] Array
Pseudocode
Step 2
We now simply need to loop through each item in the TotalCowYield [] array and add it to the TotalMilk Variable. Doing this is actually a very similar process that we followed in task 1 to first put information into the Array.
First we need to loop through each of the 7 days, for each of the 7 days we need to loop through how many cows there are. This loop structure will look like this:
TotalMilk = 0 AvgMilk = 0 Count2 = 0 FOR Day = 1 to 7 FOR x = 1 to CowHerdSize
NEXT x NEXT Day
Now that we have set up the loop structure we can now add two lines of code. The first line will add the value from the TotalCowYield [] array that we are currently on to the TotalMilk variable. The second line will add 1 to the Count2 variable so that we can move to the next position in the TotalCowYield [] array:
TotalMilk = 0 AvgMilk = 0 Count2 = 0 FOR Day = 1 to 7 FOR x = 1 to CowHerdSize TotalMilk = TotalMilk + TotalCowYield[Count2] Count2 = Count2 + 1 NEXT x NEXT Day
Pseudocode
Python Code Note - For this python example. Sample data has been hard coded into Variables and arrays that we can assume were filled in from task 1
Step 3 - Final Step
We now have the total milk yield safely stored in the TotalMilk variable. Before we show this to the user we need to divide the value by the number of times a cow was milked. This can be done by either dividing TotalMilk by Count2 because at this point Count2 will hold the number of values in the TotalMilkYield[] array. OR it can be calculated by dividing TotalMilk by len(TotalCowYield[]). Once this is calculated, we can then output the data to the user. This will take three new lines of code:
TotalMilk = 0 AvgMilk = 0 Count2 = 0 FOR Day = 1 to 7 FOR x = 1 to CowHerdSize TotalMilk = TotalMilk + TotalCowYield[Count2] Count2 = Count2 + 1 NEXT x NEXT Day
AvgMilk = TotalMilk / len(TotalCowYield []) Output “The total yield of milk for all cows is “, TotalMilk Output “ The Average yield per cow was: “, AvgMilk