Warning! It seems that you are using Dodona within another webpage, so not everything may work properly. Let your teacher know so that he can solve the problem by adjusting a setting in the learning environment. In the meantime, you can click
this link to open Dodona in a new window.
Warning! The page was not fully loaded, probably because of a network issue. It could be that not all functionalities work as expected. Please try reloading the page.
Level 9 - Two-dimension arrays and lists
0/9 0 of 9 activities solved
Rainfall collector
Not yet solved
Journey log
Not yet solved
Snakes and ladders
Not yet solved
Quote of the day
Not yet solved
Word game
Not yet solved
Grade book
Not yet solved
Checkout
Not yet solved
Missiles
Not yet solved
Noughts and crosses
Not yet solved
Next series
Assessment point - Levels 1-9
Back to course
Level 9 - Two-dimension arrays and lists
Next activity
Journey log
Sign in to hand in your solution and get feedback.
// Rainfall collector program
using System;
using System.IO;
class Submission
{
// -------------------------
// Subprograms
// -------------------------
static string[,] read_data(string filename)
{
int num_regions = 7;
string[,] data = new string[num_regions, 2];
using (StreamReader file = new StreamReader(filename))
{
for (int region = 0; region < num_regions; region++)
{
string data_row = file.ReadLine();
data_row = data_row.Trim();
// Split the row of data by the comma separator into fields
string[] data_list = data_row.Split(',');
// Populate the data table with the fields
data[region, 0] = data_list[0];
data[region, 1] = data_list[1];
}
}
return data;
}
static string padded_label(string label, int characters)
{
return label + new string(' ', characters - label.Length);
}
static void output_table(string[,] data)
{
int num_regions = 7;
int characters = 0;
for (int region = 0; region < num_regions; region++)
{
if (data[region, 0].Length > characters)
{
characters = data[region, 0].Length;
}
}
for (int region = 0; region < num_regions; region++)
{
string table_row = padded_label(data[region, 0], characters);
table_row += " | " + data[region, 1];
Console.WriteLine(table_row);
}
Console.WriteLine();
}
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
string[,] data = read_data("data.txt");
output_table(data);
}
}
Feedback