patterncsharpMinor
Census Data Display from Text File
Viewed 0 times
filecensustextdisplayfromdata
Problem
This program is for a class project and requires me to read data from a text file then display a list of totals. Here are the instructions/requirements:
The fields in each record will be separated by a comma.
Format: Age, Gender, Marital Status, District#
For example: 18, M, S, 2
The city has 22 districts. The census department wants to see a
listing of how many residents are in each district, and a count of
residents in each of the following age groups (for all the districts
combined): under 18, 18 through 30, 31 through 45, 46 through 64, and
65 or older.
The only way I can get this to work was using a bunch of
So say each time the program reads age 18 and district 2 for example, it will add 1 to Age Group 1 and 1 to District 2 that element and so on? Then at the end just display the results? I just barely learned about array and I don't know how I can do this.
```
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[] ageData = new int[900];
int[] districtDataA = new int[900];
int[] ageGroup = new int[5];
int[] districtCount = new int[22];
int i = 0;
foreach (string line in File.ReadAllLines("test.txt"))
{
string[] fields = line.Split(',');
ageData[i] = int.Parse(fields[0]);
districtDataA[i] = int.Parse(fields[3]);
if (ageData[i] > 0 && ageData[i] 18 && ageData[i] 30 && ageData[i] 45 && ageData[i] = 65)
{
ageGroup[4] = ageGroup[4] + 1;
}
//District Count info
The fields in each record will be separated by a comma.
Format: Age, Gender, Marital Status, District#
For example: 18, M, S, 2
The city has 22 districts. The census department wants to see a
listing of how many residents are in each district, and a count of
residents in each of the following age groups (for all the districts
combined): under 18, 18 through 30, 31 through 45, 46 through 64, and
65 or older.
The only way I can get this to work was using a bunch of
if statements (which is bad I've been told). Is there a way for me to just read through the data and put the correct age/district number in an array, then display the totals for the each Age Group (groups 1-5) and District (1-22)?So say each time the program reads age 18 and district 2 for example, it will add 1 to Age Group 1 and 1 to District 2 that element and so on? Then at the end just display the results? I just barely learned about array and I don't know how I can do this.
```
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[] ageData = new int[900];
int[] districtDataA = new int[900];
int[] ageGroup = new int[5];
int[] districtCount = new int[22];
int i = 0;
foreach (string line in File.ReadAllLines("test.txt"))
{
string[] fields = line.Split(',');
ageData[i] = int.Parse(fields[0]);
districtDataA[i] = int.Parse(fields[3]);
if (ageData[i] > 0 && ageData[i] 18 && ageData[i] 30 && ageData[i] 45 && ageData[i] = 65)
{
ageGroup[4] = ageGroup[4] + 1;
}
//District Count info
Solution
Try to visualize how your items are related. Think how your code runs, step by step, like in slow-mo. You will realize you can sum up long and tedious code into simplest, more abstract, styles.
Let me start from the last portion of your code:
If you find yourself writing repetitive lines as you have here, there is almost always a better way to code that. Here, a simple loop makes sense:
That makes it short and readable, less convoluted. We merely extracted the 1,2,3... and the 0,1,2... into
Now using the same "trick" you can replace all your ifs (which should have been if else's in the first place) with just a single line:
BONUS: When you want to type
Let me start from the last portion of your code:
Console.WriteLine("District 1 = {0}", districtCount[0]);
Console.WriteLine("District 2 = {0}", districtCount[1]);
Console.WriteLine("District 3 = {0}", districtCount[2]);
//etc etcIf you find yourself writing repetitive lines as you have here, there is almost always a better way to code that. Here, a simple loop makes sense:
for (int i = 1; i <= 22; i++)
{
Console.WriteLine("District {0} = {1}", i, districtCount[i-1]);
}That makes it short and readable, less convoluted. We merely extracted the 1,2,3... and the 0,1,2... into
i and i-1 respectively.Now using the same "trick" you can replace all your ifs (which should have been if else's in the first place) with just a single line:
districtCount[districtDataA[i] - 1]++;BONUS: When you want to type
variable = variable + 1 it's shorter and more readable if you type variable++ instead. Same with variable--. For different amounts, you can do variable += amount (also -=, *=, /= and more operators depending on the language)Code Snippets
Console.WriteLine("District 1 = {0}", districtCount[0]);
Console.WriteLine("District 2 = {0}", districtCount[1]);
Console.WriteLine("District 3 = {0}", districtCount[2]);
//etc etcfor (int i = 1; i <= 22; i++)
{
Console.WriteLine("District {0} = {1}", i, districtCount[i-1]);
}districtCount[districtDataA[i] - 1]++;Context
StackExchange Code Review Q#46507, answer score: 6
Revisions (0)
No revisions yet.