C# Pseudo-Code improvement/peer revision

Posted on

Problem

I have written this Pseudo-Code for the following program, I’m wondering if this might be descriptive enough for anyone to follow up and perform more or less the same program. The program reads and analyzes a set of data in a text file, then displays the information in a filtered way. Any improvements/ revision would be immensely appreciated.

PseudoCode:

  1. Module main ()
  2. //Set up format for text reading.
  3. Declare String content
  4. //Open and read file
  5. Open “Data.txt”
  6. Set contents = opened “Data.txt”
    1. //Declare an array to store and split data
  7. Declare string rows[] = contents split to lines
  8. Declare string table [][] = length of rows[]
    1. //Set loop to iterate through all values
  9. For i is 0 less than length of rows
  10. Table[] increments by 1
  11. End for
  12. //Declare arras to select and parse info
  13. []districts integer = integer [length of rows]
  14. [] ages integer = integer [length of rows]
  15. For i is 0, i is less than length of rows
  16. i increments by 1
  17. Set i of districts to integer parsing with position [i] in table[]
  18. Set i of ages to integer parsing with position [i] in table[]
  19. End for
  20. // Analyze and pass data
  21. Foreach integer in district
  22. Display “district {0} has {1} resident”, district, countofdistrict.
  23. Display “ Agegroup1 has {0} residents”, count of ages (restriction)
  24. Display “Agegroup2 has {0} residents”, count of ages (restriction)
  25. Display “ Agegroup3 has {0} residents”, count of ages (restriction)
  26. Display “ Agegroup4 has {0} residents”, count of ages (restriction)
  27. Display “ Agegroup5 has {0} residents”, count of ages (restriction)
  28. End foreach

Program:

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));
            }
        }
    }

Solution

When it comes to pseudo-code there are different standards depending on your organization, target group, etc, but here is my input:

The comments in your pseudo-code are redundant.

17 and 18 should be merged.

Variable declaration (such as 3. Declare String content) are not needed.

Not sure if every Console.WriteLine() is needed. This could be replaced with something like “Display data regarding Agegroup”

I have a suggestion for the program itself: why are you iterating over the data twice?

for (int i = 0; i < rows.Length; table[i] = rows[i].Split(','), i++) ;

And

for (int i = 0; i < rows.Length; i++)
                {
                    districts[i] = int.Parse(table[i][3]);
                    ages[i] = int.Parse(table[i][0]);
                } 

Why bother with having table be a two dimensional array when it is just a stop-gap collection to get you to ages and districts? Why not just do something like this:

string[] rows = content.Split('n'); //Each row is on a new line
int[] districts = new int[rows.Length];
int[] ages = new int[rows.Length];
string[] currentRow;

for (int i = 0; i < rows.Length; i++)
{
    currentRow = rows[i].Split(',');
    districts[i] = int.Parse(currentRow[3]);
    ages[i] = int.Parse(currentRow[0]);
}

Additionally, depending on the size of your data set, why do five different Count() executions on ages when you could just group that data before hand like this:

var ageRanges = new []{17, 30, 45, 64};

var groupedAges = ages.GroupBy(a => ageRanges.FirstOrDefault(r => r >= a));

for (var i = 0; i < ageRanges.Length; i++)
{
     var grp = groupedAges.FirstOrDefault (a => a.Key == ageRanges[i]);
     var count = grp == null ? 0 : grp.Count();
     Console.WriteLine(string.Format("Ages {0}-{1} : {2} residents", i == 0 ? 0 : ageRanges[i-1]+1, i == 0 ? ageRanges[i] + 1 : ageRanges[i], count));
}
Console.WriteLine(string.Format("Ages >= 65 : {0} residents", groupedAges.Count(g => g.Key == 0)));

Admittedly, the logic inside the loop for display purposes is a little hairy since you have different criteria (less than as opposed to less/greater than or equal) for the first age range, but that cuts down on the operations you need to do on the set of data.

Leave a Reply

Your email address will not be published. Required fields are marked *