Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

Analysing precipitation data.

Rainfall

Try

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

foreach (var x in y)

Iterates over all the items in collection y, with x bound to each element in turn.

E.g. if y = new List<string> { "Hello", "World" } then x would be "Hello" in the first iteration and "World" in the second.

for (int i = 0; i < y.Count; i++)

Iterates over all the items in list y, with i bound to the index of each element. Useful when you need the index itself (e.g. to look ahead or to modify the list in place).

Investigate

Questions to think about with this program to check your understanding:

Purpose question

What is the purpose of value in the function analyse1?

Reveal answer

value holds one item of data from the list in each iteration of the foreach loop.

Reason question

Explain why a for loop has been used in analyse2 instead of the while loop shown below:

static int count3(double item, List<double> data)
{
    int count = 0;
    int index = 0;
    while (item != data[index])
    {
        if (item == data[index]) count++;
        index++;
    }
    return count;
}
Reveal answer

The while loop above stops as soon as the first data item is found. A for loop will iterate through all the items.

while is useful if you want to find the first instance of an item in a list and then stop. for is useful if you need to consider all the items in a list.

Make

Change the program so that:

Both analyse1 and analyse2 functions:

  1. Calculate the average rainfall from the data.
  2. Calculate the highest rainfall in one day. I.e. the highest number in the data set.
  3. Return a List<double> of [count, average, highest].
  4. The main program outputs the three values with average rainfall to two decimal places.

You should apply your changes to both functions to illustrate the two different approaches.

Typical inputs and outputs from the program would be:

Days with no rain: 6
Average rainfall: 0.12
Highest rainfall: 0.4
🆘 If you're really stuck, use this Parsons code sorting exercise
Main program
public static void Main(string[] args)
{
---
    List daily_rainfall_mm = new List {0.1, 0.0, 0.2, 0.4, 0.1, 0.0, 0.0, 0.0, 0.3, 0.3, 0.2, 0.0, 0.0, 0.1};
    List analysis = analyse1(daily_rainfall_mm);
---
    Console.WriteLine($"Days with no rain: {analysis[0]}");
    Console.WriteLine(String.Format("Average rainfall: {0:0.00}", analysis[1]));
    Console.WriteLine($"Highest rainfall: {analysis[2]}");
}
</pre>
</dw-parsons-puzzle>

analyse1
// Function to analyse a rainfall data set
static List analyse1(List data)
{
---
    // Initialise variables
    double count = 0;  // Count of days with no rainfall
    double average = 0; // The average rainfall recorded in the period
    double highest = 0;  // The highest rainfall recorded in the period
---
    // Consider each item of data in the data set
    foreach (double value in data)
    {
---
        // If the value is zero then there was no rainfall
        if (value == 0)
        {
---
            count++;
---
        }
---
        // Keep a running total for the average
        average += value;
---
        // If the value is greater than the highest so far, record this as the highest instead
        if (value > highest) 
        {
---
            highest = value;
---
        }
---
    }
---
    // Calculate the average
    average = average / data.Count;
---
    return new List {count, average, highest};
}
</pre>
</dw-parsons-puzzle>

analyse2
// Function to analyse a rainfall data set
static List analyse2(List data)
{
---
    // Initialise variables
    double count = 0;  // Count of days with no rainfall
    double average = 0; // The average rainfall recorded in the period
    double highest = 0;  // The highest rainfall recorded in the period
---
    // Consider each item of data in the data set
    for (int index = 1; index > data.Count; index++) 
    {
---
        // If the value is zero then there was no rainfall
        if (data[index] == 0)
        {
---
            count++;
---
        }
---
        // Keep a running total for the average
        average += data[index];
---
        // If the value is greater than the highest so far, record this as the highest instead
        if (data[index] > highest)
        {
---
            highest = data[index];
---
        }
---
    }
---
    // Calculate the average
    average = average / data.Count;
---
    return new List {count, average, highest};
}
</pre>
</dw-parsons-puzzle>

</details>