Create an application in Visual Studio (C#) that reads the file\'s contents into
ID: 3684959 • Letter: C
Question
Create an application in Visual Studio (C#) that reads the file's contents into an array or a List. The application should display the following data:
The average annual change in population during the time period
The year with the greatest increase in population during the time period
The year with the least increase in population during the time period
These numbers are the midyear population of the United States, in thousands, during the years 1950 through 1990. The first line in the file contains the population for 1950, the second line contains the population for 1951, and so forth.
There are many ways to display the results. But I want to address a specific question about the last two bullet points: If the population in the file is the mid-year population, then is the year with the greatest increase (or lowest increase) the first or second year since the measurement happens in the middle of the year? Here are results from the Key so you can see one way to display the information:
Year Increase
1951 2114
1952 2411
1953 2563
1954 2928
1955 3185
Notice that the increase listed for 1951 is the increase from midyear 1950 to midyear 1951. Consequently, the largest increase will be the increase from 1954 to 1955 as it is shown below:
The year with the greatest increase is 1955
Explanation / Answer
frmPopulationData.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
namespace PopulationData
{
public partial class frmPopulationData : Form
{
private static Dictionary<int, int> _PopulationData;
public frmPopulationData()
{
InitializeComponent();
}
private void btnCalulate_Click(object sender, EventArgs e)
{
try
{
string[] data = File.ReadAllLines("USPopulation.txt");
if (data.Length > 0)
{
_PopulationData = new Dictionary<int, int>();
ConvertDataToInt(data);
PrintAverageAnnualPopulationGrowth();
PrintGreatestPopulationIncreaseYear();
PrintLeastPopluationIncreaseYear();
}
else
MessageBox.Show("No data found in USPopulation.txt");
}
catch (IOException)
{
MessageBox.Show("Error: USPopulation.txt not found. Place the file in the solution's bin/debug folder and try again.");
}
}
private void ConvertDataToInt(string[] populationData)
{
int year = 1950; //beginning year
for (int entry = 0; entry < populationData.Length; entry++)
{
_PopulationData.Add(year, Convert.ToInt32(populationData[entry]));
year++;
}
}
private void PrintAverageAnnualPopulationGrowth()
{
double pastYear = 0;
double totalChange = 0;
double annualPercentageChange = 0;
double totalPercentageChange = 0;
double annualAveragePercent = 0;
foreach (KeyValuePair<int, int> currentYear in _PopulationData)
{
if (currentYear.Key == 1950)
pastYear = currentYear.Value;
else
{
totalChange += currentYear.Value - pastYear;
annualPercentageChange = totalChange / pastYear;
totalPercentageChange += annualPercentageChange;
pastYear = currentYear.Value;
}
}
if (totalPercentageChange > 0)
annualAveragePercent = (totalPercentageChange / _PopulationData.Count) * 100;
txtAverage.Text = annualAveragePercent.ToString("#0.##") + "%";
}
private void PrintGreatestPopulationIncreaseYear()
{
int pastYearIncrease = 0;
int currentYearIncrease = 0;
int greatestYear = 0;
int pastYear = 0;
foreach (KeyValuePair<int, int> currentYear in _PopulationData)
{
if (currentYear.Key == 1950)
{
pastYear = currentYear.Value;
greatestYear = currentYear.Key;
}
else
{
currentYearIncrease = currentYear.Value - pastYear;
if (currentYearIncrease > pastYearIncrease)
{
pastYearIncrease = currentYearIncrease;
pastYear = currentYear.Value;
greatestYear = currentYear.Key;
}
}
}
txGreatest.Text = greatestYear.ToString();
}
private void PrintLeastPopluationIncreaseYear()
{
int pastYearIncrease = 0;
int currentYearIncrease = 0;
int leastYear = 0;
int pastYear = 0;
foreach (KeyValuePair<int, int> currentYear in _PopulationData)
{
if (currentYear.Key == 1950)
{
pastYear = currentYear.Value;
leastYear = currentYear.Key;
}
else
{
currentYearIncrease = currentYear.Value - pastYear;
if (currentYearIncrease < pastYearIncrease)
{
pastYearIncrease = currentYearIncrease;
pastYear = currentYear.Value;
leastYear = currentYear.Key;
}
}
}
txtLeast.Text = leastYear.ToString();
}
}
}
frmPopulationData.Designer.cs
namespace PopulationData
{
partial class frmPopulationData
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.txtAverage = new System.Windows.Forms.TextBox();
this.txGreatest = new System.Windows.Forms.TextBox();
this.txtLeast = new System.Windows.Forms.TextBox();
this.btnCalulate = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(31, 84);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(187, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Average annual change in population:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(31, 126);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(219, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Year with the greatest increase in population:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(31, 169);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(203, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Year with the least increase in population:";
//
// txtAverage
//
this.txtAverage.Location = new System.Drawing.Point(224, 81);
this.txtAverage.Name = "txtAverage";
this.txtAverage.Size = new System.Drawing.Size(100, 20);
this.txtAverage.TabIndex = 3;
//
// txGreatest
//
this.txGreatest.Location = new System.Drawing.Point(256, 123);
this.txGreatest.Name = "txGreatest";
this.txGreatest.Size = new System.Drawing.Size(100, 20);
this.txGreatest.TabIndex = 4;
//
// txtLeast
//
this.txtLeast.Location = new System.Drawing.Point(240, 166);
this.txtLeast.Name = "txtLeast";
this.txtLeast.Size = new System.Drawing.Size(100, 20);
this.txtLeast.TabIndex = 5;
//
// btnCalulate
//
this.btnCalulate.Location = new System.Drawing.Point(34, 32);
this.btnCalulate.Name = "btnCalulate";
this.btnCalulate.Size = new System.Drawing.Size(75, 23);
this.btnCalulate.TabIndex = 6;
this.btnCalulate.Text = "Calculate";
this.btnCalulate.UseVisualStyleBackColor = true;
this.btnCalulate.Click += new System.EventHandler(this.btnCalulate_Click);
//
// frmPopulationData
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(416, 233);
this.Controls.Add(this.btnCalulate);
this.Controls.Add(this.txtLeast);
this.Controls.Add(this.txGreatest);
this.Controls.Add(this.txtAverage);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "frmPopulationData";
this.Text = "Population Data";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtAverage;
private System.Windows.Forms.TextBox txGreatest;
private System.Windows.Forms.TextBox txtLeast;
private System.Windows.Forms.Button btnCalulate;
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PopulationData
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmPopulationData());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.