Design a modular algorith and code (C#) a solution to the problem: The city of A
ID: 3685769 • Letter: D
Question
Design a modular algorith and code (C#) a solution to the problem:
The city of Anhk-Morpork held a special census to collect data on its residents. The file containing the results of the census is structured as follows:
The fields in each record will be separated by a comma.
For example: 21, M, S, 1
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 19, 18 through 31, 31 through 45, 46 through 64, and 65 or older.
FIELD DESCRIPTION DATA TYPE VALID VALUES Age Numeric Greater than 0 Gender Character M, m, S, s Marital Status Character M, m, S, s District Numeric 1-22Explanation / Answer
using System;
using System.IO;
public class Sample
{
public static void Main() {
int[] Age;
char[] Gender;
char[] MaritalStatus;
int[] District;
int choice, count =0,under19 =0,under31 =0,under45 =0,under64 =0,age65 =0;
using (StreamReader reader = new StreamReader("yourfile.txt")) {
string line = null;
for (int i=0; null != (line = reader.ReadLine()); i++) {
string[] values = line.Split(',');
Age[i] =int.Parse(values[0]);
Gender[i] = char.Parse(values[1];
MaritalStatus[i] = char.Parse(value[2]);
District[i] = int.Parse(value[3]);
}
}
Console.WriteLine(" A count of residents in each of the following age groups (for all the districts combined):");
for(int i= 0; i <Age.Length -1; i++){
if (age[i] < 19)
under19++;
if (age[i] > 18 && age[i] <= 31)
under31++;
if (age[i] > 31 && age[i] <= 45)
under45++;
if (age[i] > 45 && age[i] <= 64)
under64++;
if (age[i] >= 65)
age65++;
}
Console.WriteLine(" Under 19: {0}",under19);
Console.WriteLine(" 18 through 31: {0}", under31);
Console.WriteLine(" 31 through 45: {0}", under45);
Console.WriteLine(" 45 through 64: {0}", under64);
Console.WriteLine(" 65 or Older: {0}", age65);
Console.WriteLine("Enter District number(from 1 to 22) You want to see a listing of how many residents:");
choice = Console.Read();
for(int i= 0; i < 22; i++)
if(District[i] == ch)
count++;
Console.WriteLine("A list of Residents in District {0} are {0}", choice, count);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.