HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Summarizing a CSV file containing census data

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
containingfilecensuscsvsummarizingdata

Problem

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Project2
{
class Program
{
static void Main(string[] args)
{
int[] ageForPeople = new int[10000];
int[] DistrictForCensus = new int[10000];
int[] agesForCensusGroups = new int[5];
int[] maxDistrictAmount = new int[22];
int placeValueHolder = 0;
foreach (string line in File.ReadAllLines("census.txt"))
{
string[] fields = line.Split(',');

ageForPeople[placeValueHolder] = int.Parse(fields[0]);
DistrictForCensus[placeValueHolder] = int.Parse(fields[3]);

if (ageForPeople[placeValueHolder] > 0 && ageForPeople[placeValueHolder] 18 && ageForPeople[placeValueHolder] 30 && ageForPeople[placeValueHolder] 45 && ageForPeople[placeValueHolder] = 65)
{
agesForCensusGroups[4]++;
}

//District Count info
if (DistrictForCensus[placeValueHolder] == 1)
{
maxDistrictAmount[0]++;
}
if (DistrictForCensus[placeValueHolder] == 2)
{
maxDistrictAmount[1]++;
}
if (DistrictForCensus[placeValueHolder] == 3)
{
maxDistrictAmount[2]++;
}
if (DistrictForCensus[placeValueHolder] == 4)
{
maxDistrictAmount[3]++;
}
if (DistrictForCensus[placeValueHolder] == 5)
{
maxDistrictAmount[4]++;
}
if (DistrictForCensus[placeValueHolder] == 6)
{
maxDistrictAmount[5]++;
}
if (DistrictForCensus[placeValueHolder] ==

Solution

-
The first if group can be slightly simplified using the else if:

if (ageForPeople[placeValueHolder] <= 0)
{
    // Trigger error.
}
else if (ageForPeople[placeValueHolder] <= 18)
{
    agesForCensusGroups[0]++;
}
else if (ageForPeople[placeValueHolder] <= 30)
{
    agesForCensusGroups[1]++;
}
else if (ageForPeople[placeValueHolder] <= 45)
{
    agesForCensusGroups[2]++;
}
else if (ageForPeople[placeValueHolder] <= 64)
{
    agesForCensusGroups[3]++;
}
else
{
    agesForCensusGroups[4]++;
}


-
The second if group can be replaced with the following single line:

maxDistrictAmount[DistrictForCensus[placeValueHolder] - 1]++;


To make sure that DistrictForCensus[placeValueHolder] is within the allowed bounds:

// District Count info
int district = DistrictForCensus[placeValueHolder];
if (district >= 1 && district <= 22)
{
    maxDistrictAmount[district - 1]++;
}


  • Side note. Do not use capitalized names for local variables. Rename DistrictForCensus to districtForCensus.

Code Snippets

if (ageForPeople[placeValueHolder] <= 0)
{
    // Trigger error.
}
else if (ageForPeople[placeValueHolder] <= 18)
{
    agesForCensusGroups[0]++;
}
else if (ageForPeople[placeValueHolder] <= 30)
{
    agesForCensusGroups[1]++;
}
else if (ageForPeople[placeValueHolder] <= 45)
{
    agesForCensusGroups[2]++;
}
else if (ageForPeople[placeValueHolder] <= 64)
{
    agesForCensusGroups[3]++;
}
else
{
    agesForCensusGroups[4]++;
}
maxDistrictAmount[DistrictForCensus[placeValueHolder] - 1]++;
// District Count info
int district = DistrictForCensus[placeValueHolder];
if (district >= 1 && district <= 22)
{
    maxDistrictAmount[district - 1]++;
}

Context

StackExchange Code Review Q#85921, answer score: 6

Revisions (0)

No revisions yet.