IGCSE / GCSE Computer Science

June 2018 Pre released material - 22

With Task 1 now complete, it is now time to tackle task 2.

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

The total milk from all cows over 7 days to 0 Decimal places -

The Average yield per cow over 7 days per cow -

Assumptions

We are to assume that task 1 is complete and all data has been collected

The following program flow steps give a brief outline of the key steps involved in task 2.

First we will need a new

Next we need to declare a new

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

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)

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:

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:

Pseudocode

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

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

Count2 = Count2 + 1

Pseudocode

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

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

Count2 = Count2 + 1

AvgMilk = TotalMilk / len(TotalCowYield [])

Pseudocode

Est. 2015 - Copyright © 2020