Code a C# project (using Visual Studio 2013 or 2015) to solve the following prob
ID: 3703467 • Letter: C
Question
Code a C# project (using Visual Studio 2013 or 2015) to solve the following problem:
The city of Ankh-Morpork held a special census to collect data on its residents.
The file containing the results of the census is structured as follows:
FIELD DESCRIPTION
DATA TYPE
VALID VALUES
Age
Numeric
Greater than 0
Gender
Character
M, m, F, f
Marital Status
Character
M, m, S, s
District
Numeric
residents are in each district, and a count of residents in each of the following age
The city has 22 districts. The census department wants to see a listing of how many
For example: 21, M, S, 1The fields in each record will be separated by a comma.
groups (for all the districts combined): under 18, 18 through 30, 31 through 45, 46
through 64, and 65 or older.
Example Output:
District
Population
1 ,423
2,123
.
.
.
.
22, 187
Under 18, 754
18-30, 622
31-45, 852
46-64, 543
65 & over, 126
FIELD DESCRIPTION
DATA TYPE
VALID VALUES
Age
Numeric
Greater than 0
Gender
Character
M, m, F, f
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.