This is a simple rainfall analysis programme that counts the number of days with no rain from the data it supplied.
00:10
Let's set up the boilerplate.
00:12
As we're going to use a list to store the rainfall data, we need to tell C we're using the System Collections generic library of commands.
00:23
In our main programme section, we'll have a list of double numbers.
00:27
Remember, that's numbers with decimal places called dailyrainfallmm We'll tell C this is a new list of double numbers and enumerate the list with some data inside curly brackets.
00:43
Of course, in a real application it would be reading this data from the rainfall sensor's memory.
00:48
These numbers are millimetres of rain in a day, and we've collected 14 days worth of data.
00:56
Next, we'll output two messages information for the user and then days with no rain concatenated to the return value from a function we'll write in a moment called Analysis 1, which will require the data from our daily rainfall list.
01:14
We're going to write this programme in two ways.
01:17
So we'll repeat that bit of code, but this time using the analysis to function that we'll write in a moment.
01:26
Okay, so define the analysis 1 function in the subprogram section.
01:31
This will return a double number and require some data.
01:35
So we'll call the parameter data, which will be our list of double numbers.
01:40
Note how the values from daily rainfallmm are parsed into this new list.
01:46
We could have used a global list instead, but this is a better way of writing the programme.
01:52
The count of the number of days with no rainfall is stored in a variable identified as count.
01:58
That will also be a double number.
02:00
But thinking about it, we maybe should have used an integer here.
02:04
we'll initialise that to zero.
02:08
Now we want to iterate through all the elements in data.
02:11
Here's another use for the for loop.
02:14
For each double value in data, if value is the same as zero, then add one to count.
02:23
Finally, outside of the loop, return the value of count back to the main programme.
02:30
Hopefully you can see how useful a for loop is.
02:33
Here.
02:33
The statement almost reads as it would in English.
02:36
For each double value in data, if there was no rain, add one to the daily count.
02:43
So in the subprogram you can see how it's possible to iterate through all the elements in a list or an array using a for loop, but without referring to the index.
02:53
However, let's write the same subprogram that way too, so you can see the difference, even though the output will be the same.
03:01
Here's a second function called analysis2.
03:05
It's the same as analysis1, except when we get to the for loop, we will use the index instead.
03:12
You can see it's slightly more complicated when we iterate by index compared to iterating by element.
03:18
This is really common in programming.
03:20
There are hundreds of ways to write the same programme.
03:23
There's often not a correct answer.
03:25
If it works, it works.
03:27
However, we usually want the most efficient solution in terms of readability of the code for humans.
03:32
So in this case, analysis 1 is easier to understand in terms of efficiency of execution and memory footprint.
03:41
Both of these solutions are pretty much the same.
03:44
Once the code is compiled, let's run the programme and we can see the same output from both subprograms.