Question

C# Visual Studio Problem You are given a file named USPopulation.txt. The file contains the midyear...

C# Visual Studio Problem

You are given a file named USPopulation.txt. The file contains 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. Create an application that reads the file’s contents into an array or a List.

The application should display the following data (statistics) in read-only textboxes. The average population during the time period. The largest population during the time period. The average annual change in population during the time period.

You are also required to: Create a form that Provide a button that users can click to obtain the population statistics. Allow the user to clear the data (statistics) of the form. Allow the user to close the form.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Create a C# windows application with the name PopulationStatistics. Add the below code in respective files.

Form1.Designer.cs

namespace PopulationStatistics
{
    partial class Form1
    {
        /// <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.panel1 = new System.Windows.Forms.Panel();
            this.lblAvgPopulation = new System.Windows.Forms.Label();
            this.txtAvgPopulation = new System.Windows.Forms.TextBox();
            this.lblLargestPopulation = new System.Windows.Forms.Label();
            this.txtLargestPopulation = new System.Windows.Forms.TextBox();
            this.lblAnnualChange = new System.Windows.Forms.Label();
            this.txtAvgAnnualChange = new System.Windows.Forms.TextBox();
            this.btnGetStatistics = new System.Windows.Forms.Button();
            this.btnClear = new System.Windows.Forms.Button();
            this.btnClose = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            //
            // panel1
            //
            this.panel1.Controls.Add(this.txtAvgAnnualChange);
            this.panel1.Controls.Add(this.lblAnnualChange);
            this.panel1.Controls.Add(this.txtLargestPopulation);
            this.panel1.Controls.Add(this.lblLargestPopulation);
            this.panel1.Controls.Add(this.txtAvgPopulation);
            this.panel1.Controls.Add(this.lblAvgPopulation);
            this.panel1.Location = new System.Drawing.Point(6, 15);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(465, 136);
            this.panel1.TabIndex = 0;
            //
            // lblAvgPopulation
            //
            this.lblAvgPopulation.AutoSize = true;
            this.lblAvgPopulation.Location = new System.Drawing.Point(21, 17);
            this.lblAvgPopulation.Name = "lblAvgPopulation";
            this.lblAvgPopulation.Size = new System.Drawing.Size(251, 17);
            this.lblAvgPopulation.TabIndex = 0;
            this.lblAvgPopulation.Text = "Average population during the period: ";
            //
            // txtAvgPopulation
            //
            this.txtAvgPopulation.Location = new System.Drawing.Point(278, 14);
            this.txtAvgPopulation.Name = "txtAvgPopulation";
            this.txtAvgPopulation.ReadOnly = true;
            this.txtAvgPopulation.Size = new System.Drawing.Size(150, 23);
            this.txtAvgPopulation.TabIndex = 1;
            //
            // lblLargestPopulation
            //
            this.lblLargestPopulation.AutoSize = true;
            this.lblLargestPopulation.Location = new System.Drawing.Point(21, 54);
            this.lblLargestPopulation.Name = "lblLargestPopulation";
            this.lblLargestPopulation.Size = new System.Drawing.Size(246, 17);
            this.lblLargestPopulation.TabIndex = 2;
            this.lblLargestPopulation.Text = "Largest population during the period: ";
            //
            // txtLargestPopulation
            //
            this.txtLargestPopulation.Location = new System.Drawing.Point(278, 51);
            this.txtLargestPopulation.Name = "txtLargestPopulation";
            this.txtLargestPopulation.ReadOnly = true;
            this.txtLargestPopulation.Size = new System.Drawing.Size(150, 23);
            this.txtLargestPopulation.TabIndex = 3;
            //
            // lblAnnualChange
            //
            this.lblAnnualChange.AutoSize = true;
            this.lblAnnualChange.Location = new System.Drawing.Point(21, 95);
            this.lblAnnualChange.Name = "lblAnnualChange";
            this.lblAnnualChange.Size = new System.Drawing.Size(252, 17);
            this.lblAnnualChange.TabIndex = 4;
            this.lblAnnualChange.Text = "Average annual change in population: ";
            //
            // txtAvgAnnualChange
            //
            this.txtAvgAnnualChange.Location = new System.Drawing.Point(278, 92);
            this.txtAvgAnnualChange.Name = "txtAvgAnnualChange";
            this.txtAvgAnnualChange.ReadOnly = true;
            this.txtAvgAnnualChange.Size = new System.Drawing.Size(150, 23);
            this.txtAvgAnnualChange.TabIndex = 5;
            //
            // btnGetStatistics
            //
            this.btnGetStatistics.Location = new System.Drawing.Point(30, 191);
            this.btnGetStatistics.Name = "btnGetStatistics";
            this.btnGetStatistics.Size = new System.Drawing.Size(100, 25);
            this.btnGetStatistics.TabIndex = 6;
            this.btnGetStatistics.Text = "Get Statistics";
            this.btnGetStatistics.UseVisualStyleBackColor = true;
            this.btnGetStatistics.Click += new System.EventHandler(this.btnGetStatistics_Click);
            //
            // btnClear
            //
            this.btnClear.Location = new System.Drawing.Point(198, 191);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(75, 25);
            this.btnClear.TabIndex = 7;
            this.btnClear.Text = "Clear";
            this.btnClear.UseVisualStyleBackColor = true;
            this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            //
            // btnClose
            //
            this.btnClose.Location = new System.Drawing.Point(359, 191);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(75, 25);
            this.btnClose.TabIndex = 8;
            this.btnClose.Text = "Close";
            this.btnClose.UseVisualStyleBackColor = true;
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(483, 244);
            this.Controls.Add(this.btnGetStatistics);
            this.Controls.Add(this.btnClear);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.panel1);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "Form1";
            this.Text = "Population Statistics";
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.TextBox txtAvgAnnualChange;
        private System.Windows.Forms.Label lblAnnualChange;
        private System.Windows.Forms.TextBox txtLargestPopulation;
        private System.Windows.Forms.Label lblLargestPopulation;
        private System.Windows.Forms.TextBox txtAvgPopulation;
        private System.Windows.Forms.Label lblAvgPopulation;
        private System.Windows.Forms.Button btnGetStatistics;
        private System.Windows.Forms.Button btnClear;
        private System.Windows.Forms.Button btnClose;
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PopulationStatistics
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGetStatistics_Click(object sender, EventArgs e)
        {
            //create a list to hold the population read from the file
            List<int> population = new List<int>();
            int totalPopulation = 0;
            //open the file to read the data , replace the below path with the path of your file
            var fileStream = new FileStream(@"E:\DotNet\PopulationStatistics\PopulationStatistics\USPopulation.txt", FileMode.Open, FileAccess.Read);
            using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
            {
                string line;
                //run a loop until end of file is reached
                while ((line = streamReader.ReadLine()) != null)
                {
                    //add each line read to the list
                    population.Add(Convert.ToInt32(line));
                }
            }

            //create a list to hold the change in the population
            List<int> changeInPopulation = new List<int>();
            //assume the first population to be largest
            int largest = population[0];
            //run a loop to iterate through the list
            for(int i = 0; i < population.Count; i++)
            {
                //compare each population to the largest
                if (population[i] > largest)
                    largest = population[i];
                //compute the running total of the population
                totalPopulation = totalPopulation + population[i];
                //get the change in population
                if (i == 0)
                    changeInPopulation.Add(0);
                else
                    //store the change in population in the list
                    changeInPopulation.Add(population[i] - population[i - 1]);
            }
            //compute the average population
            double avgPopulation = Convert.ToDouble(totalPopulation) / population.Count();
            //compute the annual average change in population
            int totalChange = 0;
            double avgAnnualChange;
            //run a loop to compute the total change in population
            for (int i = 0; i < changeInPopulation.Count(); i++)
                totalChange = totalChange + changeInPopulation[i];
            //compute the average annual change in population
            avgAnnualChange = Convert.ToDouble(totalChange) / changeInPopulation.Count();

            //display the results
            txtAvgPopulation.Text = avgPopulation.ToString("0.00");
            txtLargestPopulation.Text = largest.ToString();
            txtAvgAnnualChange.Text = avgAnnualChange.ToString("0.00");
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtAvgPopulation.Text = "";
            txtLargestPopulation.Text = "";
            txtAvgAnnualChange.Text = "";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Output:

Code Screenshots:

Add a comment
Know the answer?
Add Answer to:
C# Visual Studio Problem You are given a file named USPopulation.txt. The file contains the midyear...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT