Analysing precipitation data.
Watch this video to learn about the new concepts shown in the program:
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).
Questions to think about with this program to check your understanding:
What is the purpose of value in the function analyse1?
value holds one item of data from the list in each iteration of the foreach loop.
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;
}
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.
Change the program so that:
Both analyse1 and analyse2 functions:
List<double> of [count, average, highest].You should apply your changes to both functions to illustrate the two different approaches.
Days with no rain: 6
Average rainfall: 0.12
Highest rainfall: 0.4
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>