TASK 1 – Record the yield. Write a program for TASK 1 to record the milk yields for a week. The program records and stores the identity code number and the yield every time a cow is milked.
Task 1 simply requires us to collect the ID numbers of all cows, ensure that they are only 3 digits long and unique and then record the milk yield for each cow twice a day for 7 days.
Program Flow
The following program flow steps give a brief outline of the key steps involved in task 1.
Step 1 Ask the farmer and record the number of cows in his heard Store this information in a variable of integer data type - Name = CowHerdSize
Step 2 Record and validate a unique id number for each cow that is also 3 digits long ID numbers to be stored in an array - name = CowId [] After each id is entered, you must check 2 things before placing it into the CowID [] array, these are:
1) that the ID is 3 digits long 2) That the ID is not already in the CowId [] array
Step 2 Record the milk yield for each cow, twice a day for a 7 day week. An array will be required to record the milk yields for each cow, each day - name TotalCowYield [] Two variables are required to store the milk yield from the 2 daily yields - Names = Session1 & Session1 Both sessions are recorded for each day, they are added up and the total placed into the TotalCowYield [] Array. 2 loops will be required here - one to loop through the 7 days, within this day loop we need another loop to loop through every cow.
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: We need to know how many cow there are in the herd. This will be stored in a Variable of Integer data type. Below we will do this by first declaring a variable called CowHerdSize and then asking the user to enter the number of cows. The number that the user enters is to be stored in the CowHerdSize variable:
Pseudocode
Python code:
Step 2 Now that we know how many cows are in the herd, we can create an array (Of the correct size) to store their ID numbers. Whilst we are creating arrays, we will also create one ready to store the how much milk each cow yields during each milking session.
Pseudocode
Python code:
Step 3 Now that we know how many cows are in the herd, and we have a place to store their ID numbers, it is time to start collecting them.
This process is going to call for a loop within a loop.
The first (Main loop) will loop for as many times as we have cows. When you know how many times a loop is required it is standard to use a FOR loop. This would look something like this: For x = 1 to CowHerdSize
End For The Second Loop will be responsible for allowing the user to enter an ID number again IF they enter an incorrect one the first time. For this we will use a REPEAT... UNTIL loop. This will fit inside the for loop and will look like this
For x = 1 to CowHerdSize Repeat
Until Len(CowId[x] = 3) End For
Now within the Repeat until loop we need to ask the user to enter a 3 digit ID number, check if it is 3 digits long. If it is Not 3 digits long then the program will not be able to progress through to the next cow (IE the next step of the FOR loop. Instead, the Repeat until loop will run again and allow the user to enter a different ID no for the same cow. The code for this should look like:
For x = 1 to CowHerdSize Repeat OUTPUT " Please enter a 3 digit cow ID no" CowId[x] = Input IF len(CowId[x]) < 3 THEN OUTPUT "INVALID cow ID, Please ensure that it has 3 digits" END IF Until Len(CowId[x] = 3) End For
Our code so far now looks like this:
Pseudocode
Python Code: Please note that the last line of this code (Line 21) is not required. It has been added so that you can see the contents of the CowID Array.
Ensuring a unique ID number
Next we need to look at each new ID as we receive it and compare it to the already stored ID numbers. If we find a match, it is not unique and we will need to reject it. If there is no match, it is unique and we can accept it.
For this task we are going to need another FOR loop that will loop as many times as there are values in the TotalCowYield [] array:
FOR y = 1 to CowId[count]
NEXT y
the next step is to add a selection statement within the for loop that will check the newly added cow id against the value in the CowID [] array position y. We also need to include a new variable called "Accepted" this will be set to false if the ID is found to be a duplicate or True if the ID is found to be unique - This Variable will then become part of the repeat until loop that decides if the ID no is accepted.
Accepted = False FOR y = 1 to CowId[count] IF CowId[y] = CowId[x] OUTPUT "This ID no is taken, please try again" Accepted = False ELSE Accepted = True NEXT y
This block of code can now be added to our original block of code. we will look to place it after we have checked that the ID is 3 digits long. Take note of how the repeat until loop will now repeat until 2 conditions are true - Length of the ID is 3 AND ID is unique:
Accepted = False For x = 1 to CowHerdSize Repeat OUTPUT " Please enter a 3 digit cow ID no" CowId[x] = Input IF len(CowId[x]) < 3 THEN OUTPUT "INVALID cow ID, Please ensure that it has 3 digits" END IF FOR y = 1 to CowId[count] IF CowId[y] = CowId[x] OUTPUT "This ID no is taken, please try again" Accepted = False ELSE Accepted = True NEXT y Until Len(CowId[x] = 3 AND Accepted = True) End For
Python Code Note that REPEAT UNTIL loops do not exist in python, instead a while loop has been used below along with other python functions.
Step 4 - Final step Now that we have the cow ID numbers stored, We can begin to record and store the milk yields for each cow, twice a day for 7 days...
We know that we need to record data for 7 days. This means that we can start by creating a for loop that will loop 7 times. For practicality, lets call the FOR counter "day". Within this loop we can add a prompt telling the user to enter data for the day that we are working with.
FOR day = 1 to 7
OUTPUT " Please enter data for day " , day
NEXT Day
Now, within the for loop, for each day of the week, we want to enter the yield for each cow. To do this we need another for loop, this time it will loop for every cow we have e.g. if we have 5 cows, it will repeat 5 times.
FOR day = 1 to 7
OUTPUT " Please enter data for day " , day FOR x = 1 to CowHerdSize
Next x NEXT Day
Now within the second FOR loop we need to collect the milk yield for 2 separate milk sessions. Once we have these, we can add them together before placing the total into the TotalCowYield array.
For this to work, we now need to declare 3 more variables. For good practice, these should be added to the top of your program.
We need: Session1 - To store the yield from the day's first session Session2 - To store the yield from the day's second session Count - Used to ensure that data is placed into the correct position in the TotalCowYield[] Array
Once we have these declared, we can set up our algorithm as follows:
Session1 = 0 Session2 = 0 Count = 0
FOR day = 1 to 7 OUTPUT " Please enter data for day " , day
FOR x = 1 to CowHerdSize OUTPUT "Please enter milk from", CowId[x], "For Session 1" Session1 = ROUND(INPUT, 2DP) OUTPUT "Please enter milk from", CowId[x], "For Session 2" Session2 = ROUND(INPUT, 2DP) TotalCowYield[Count] = Session1 + Session2 Count = Count + 1 Next x NEXT Day
You should also take notice, The 2 inputs in the below function make use of the round function to ensure that the milk yield is stored to 2 decimal places, this was an important task requirement.
Final Pseudocode - (Task 1)
Final Python code (Task1):
Again Lines 23 and 41 have been added for testing purposes, they are there to allow you to see what is being stored in each array once we are done with them.
Data Storage To ensure that you fully understand the above solution to task 1 we need to take a look at how the data is being stored in the TotalCowYield [] Array.
Lets produce some test data.
We have 2 cows Cow 1 ID = 111 Cow 2 ID = 222
Data is collected in the following order. Green values are stored temporarily and added to make the total - Red value. The red values are then placed into the TotalCowYield [] Array.
Day 1 Cow 111 - Session 1 recorded = (12 Litres) Cow 111 - Session 2 recorded = (16 Litres) Cow 111 Session 1 added to session 2 - Total is placed into the 1st array position = (28 Litres) Cow 222 - Session 1 = (13 Litres) Cow 222 Session 2 = (17 Litres) Cow 222 Session 1 added to Session 2 - Total is placed into the 2nd array position = (30 Litres)
Day 2 Cow 111 - Session 1 recorded = (14 Litres) Cow 111 - Session 2 recorded = (3 Litres) Cow 111 Session 1 added to session 2 - Total is placed into the 3rd array position = (17 Litres) Cow 222 - Session 1 = (10 Litres) Cow 222 Session 2 = (10 Litres) Cow 222 Session 1 added to Session 2 - Total is placed into the 4th array position = (20 Litres)
Day 3 Cow 111 - Session 1 recorded = (16 Litres) Cow 111 - Session 2 recorded = (7 Litres) Cow 111 Session 1 added to session 2 - Total is placed into the 5th array position = (23 Litres) Cow 222 - Session 1 = (19 Litres) Cow 222 Session 2 = (10 Litres) Cow 222 Session 1 added to Session 2 - Total is placed into the 6th array position = (29 Litres)
Day 4 Cow 111 - Session 1 recorded = (20 Litres) Cow 111 - Session 2 recorded = (20 Litres) Cow 111 Session 1 added to session 2 - Total is placed into the 7th array position = (40 Litres) Cow 222 - Session 1 = (19Litres) Cow 222 Session 2 = (17 Litres) Cow 222 Session 1 added to Session 2 - Total is placed into the 8th array position = (36 Litres)
Day 5 Cow 111 - Session 1 recorded = (5 Litres) Cow 111 - Session 2 recorded = (6 Litres) Cow 111 Session 1 added to session 2 - Total is placed into the 9th array position = (11 Litres) Cow 222 - Session 1 = (16 Litres) Cow 222 Session 2 = (16 Litres) Cow 222 Session 1 added to Session 2 - Total is placed into the 10th array position = (32 Litres)
Day 6 Cow 111 - Session 1 recorded = (14 Litres) Cow 111 - Session 2 recorded = (13 Litres) Cow 111 Session 1 added to session 2 - Total is placed into the 11th array position = (27 Litres) Cow 222 - Session 1 = (9 Litres) Cow 222 Session 2 = (6 Litres) Cow 222 Session 1 added to Session 2 - Total is placed into the 12th array position = (15 Litres)
Day 7 Cow 111 - Session 1 recorded = (1 Litres) Cow 111 - Session 2 recorded = (3 Litres) Cow 111 Session 1 added to session 2 - Total is placed into the 13th array position = (4 Litres) Cow 222 - Session 1 = (19 Litres) Cow 222 Session 2 = (5 Litres) Cow 222 Session 1 added to Session 2 - Total is placed into the 14th array position = (24 Litres)
Once all of these vales have been added, the program will finish, the data in the array will look like this:
[28,30,17,20,23,29,40,36,11,32,27,15,4,24]
This data can be put into a table to help visualize what order it is in, see the image below: