I am fairly new to programming and I am having difficulty modulating the following code.
The program reads a file, selects certain requirements, and displays them. I have tried to use passing arrays as arguments or functions as indicated in my textbook. I have been using examples such as int getAges(int array[], integer)
.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Project_2_practice
{
class Program
{
static void Main(string[] args)
{
//Parsing data into memory
string content;
using (StreamReader reader = new StreamReader(File.Open("Data.txt", FileMode.Open)))
{
content = reader.ReadToEnd();
}
string[] rows = content.Split('\n'); //Each row is on a new line
string[][] table = new string[rows.Length][];
for (int i = 0; i < rows.Length; table[i] = rows[i].Split(','), i++) ;
//selecting information
int[] districts = new int[rows.Length];
int[] ages = new int[rows.Length];
for (int i = 0; i < rows.Length; i++)
{
districts[i] = int.Parse(table[i][3]);
ages[i] = int.Parse(table[i][0]);
}
//Analyzing selected information
foreach (int district in districts.Distinct().OrderBy(x => x))
Console.WriteLine("District {0} has {1} resident(s)", district, districts.Count(x => x == district));
Console.WriteLine("Ages 0-18 : {0} resident(s)", ages.Count(x => x < 18));
Console.WriteLine("Ages 18-30 : {0} resident(s)", ages.Count(x => x >= 18 && x <= 30));
Console.WriteLine("Ages 31-45 : {0} resident(s)", ages.Count(x => x >= 31 && x <= 45));
Console.WriteLine("Ages 46-64 : {0} resident(s)", ages.Count(x => x >= 46 && x <= 64));
Console.WriteLine("Ages >=65 : {0} resident(s)", ages.Count(x => x >= 65));
}
}
}