Rainfall collector

Rainfall collector

Formatting rainfall data.

Try


Select the button below to open the C# program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

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

Knowledge Organiser

int[,] w = new int[x, y];

In this example, w is declared as a 2D array of integers with x rows and y columns.

Columns and rows don't really exist in the computer memory. They are just an abstraction that makes it easier for us to understand. Whether x is a row or y is a row doesn't matter. By convention, in this example x would be a row but how you visualise the table is up to you.

List<List<int>> w = new List<List<int>>();

In this example, w is declared as a 2D list of integers.


Reading CSV files


Data can be stored in comma separated file (CSV) format, where each row (record) has data separated into columns (fields) by commas.


If the number of rows and columns is known, you can use a 2D array initialised with two for loops as shown above. If the number of rows is not known, a 2D list will be needed with new rows added to the list as they are read from the file.


This is a common operation when using files and 2D lists. Here is some boilerplate code that can be used to read data from a file and return it in a 2D list called database.


static List<List<string>> ReadFile(string filename)

{

List<List<string>> database = new List<List<string>>();

StreamReader file = new StreamReader(filename);

string line;

while ((line = file.ReadLine()) != null)

{

string[] fields = line.Split(',');

for (int i = 0; i < fields.Length; i++)

{

fields[i] = fields[i].Trim();

}

List<string> fieldList = new List<string>(fields);

database.Add(fieldList);

}

file.Close();

return database;

}

Investigate


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

Item question

Identify the line where a 2D array is initialised.

REVEAL ANSWER

Line 14:

string[,] data = new string[num_regions, 2];

Structure question

Explain what the numbers 7 and 2 represent in the code in lines 13 and 14.

REVEAL ANSWER

The number of rows and columns when the two-dimension structure is visualised as a table. Two columns and seven rows.

As the structure is zero-indexed, the first index is 0, so the indexes for seven rows are 0-6, and the columns 0-1.

Index
0
1
0

data[0,0]

data[0,1]

1

data[1,0]

data[1,1]

2

data[2,0]

data[2,1]

3

data[3,0]

data[3,1]

4

data[4,0]

data[4,1]

5

data[5,0]

data[5,1]

6

data[6,0]

data[6,1]

Make


Success Criteria

Change the program so that it:

  1. Includes a function called `short_label` that takes a parameter `label` and returns the first character from each word in label. E.g. South West would be SW. Hint: splitting the label into a list of words separated by a space and using a for loop to iterate the list extracting the first character is the easiest way to solve this problem.
  2. Includes a subprogram called, `output_short_table` that outputs the data in the format shown below.
  3. Calls the `output_short_table` subprogram after `output_table` in the main program.

Typical inputs and outputs from the program would be:

NW | 30

NE | 31

C | 13

E | 10

SE | 17

SW | 18

IoW | 16

Help

Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.

Evaluate


Compare the output from your program with the expected output below to check that your program has met the success criteria.

North West | 30

North East | 31

Central | 13

East | 10

South East | 17

South West | 18

Isle of Wight | 16


NW | 30

NE | 31

C | 13

E | 10

SE | 17

SW | 18

IoW | 16

Registered in England and Wales: 10442992

VAT Number: 290 9845 58

Telephone: 01452 947500

Email: admin@craigndave.co.uk

Bett Awards 2024 Finalist