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
  • The attached file US Population.txt contains the US population in thousands, during the years 1950 through...

    The attached file US Population.txt contains the US population 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. Write a program that reads the file's contents into a list. The program should display the following data: the average annual change in population during the time period. You will notice the population numbers are always increasing. So rather than finding...

  • In Visual Basic. Thank you in advance! Visual Basic File access Create a form that allow...

    In Visual Basic. Thank you in advance! Visual Basic File access Create a form that allow users to enter information (either through text boxes or user input). You decide what you want the users to enter. Use the information entered by the user to create a file that contains the information entered.

  • Hey there, I am having a hard time using GUI for c# in visual studio. I...

    Hey there, I am having a hard time using GUI for c# in visual studio. I am making an alarm clock. I have a few questions that I would like to know how to do: --using a button called set time to capture text from a box containing a user-inputted hour, minute, second, and setting the current time to that. --using a button called alarm, Capture the Text in the TextBoxes - the Hours, Minutes, Seconds and Text (am/pm) and...

  • In Python Please use the same USPopulation.txt file from Project 8 as input data. For this project, read the data into a list of lists (a 2-dimensional list, 4 decades, each decade with 10 years). Com...

    In Python Please use the same USPopulation.txt file from Project 8 as input data. For this project, read the data into a list of lists (a 2-dimensional list, 4 decades, each decade with 10 years). Compute the annual change in the population, and the year with the maximum and minimum population change. Using Anaconda, plot a graph for each decade's population change. The attached file USPopulation.txt contains the US population in thousands, during the years 1950 through 1990. The first...

  • Mike is a consultant for the Department of Labor. One day he is analyzing the midyear...

    Mike is a consultant for the Department of Labor. One day he is analyzing the midyear population of the United States, in thousands, during the years 1950 through 1990. He write Population Class in java including a main method as the testing client. His next task is to complete the entire class by adding: 1. A private field that can store a yearly population data as an integer array. 2. A constructor that can instantiate a Population object by using...

  • The questions below deal with Microsoft Visual Studio 2012 Reloaded, under the Visual Basic programming language....

    The questions below deal with Microsoft Visual Studio 2012 Reloaded, under the Visual Basic programming language. The book ISBN number that I am using is: 978-1-285-08416-9 or the UPC code is: 2-901285084168-1. Question 1 Visual Basic 2012 is an object-oriented programming language, which is a language that allows the programmer to use ____________________ to accomplish a program�s goal. Question 2 An application that has a Web user interface and runs on a server is called a(n) ____________________ application. Question 3...

  • write a programming code for the following problem using Visual Basic Studio VB. Write a program that will allow the user to enter series of mumbers and will give some useful statistics about the mam...

    write a programming code for the following problem using Visual Basic Studio VB. Write a program that will allow the user to enter series of mumbers and will give some useful statistics about the mambers Enter Numbers Button Click-Allow the user to enter series of nambers using imput dialog boxes. Use a loop and add the numbers to the listBox as they are input.. Prompt the user to enter"x" to stop addang numbers. Compute Statistics Button Click-Compute the following statistics...

  • C# Visual Studio -The local driver's license office has asked you to create an application that...

    C# Visual Studio -The local driver's license office has asked you to create an application that grades the written portion of the driver's license exam. The exam has 20 multiple choice questions. Here are the correct answers: 1. B 2. D 3. A 4. A 5. C 6. A 7. B 8. A 9. C 10. D 11. B 12. C 13. D 14. A 15. D 16. C 17. C 18. B 19. D 20. A Your program should...

  • Visual Studio Code C# Well Document everyline saying what the code does. Include designer code and...

    Visual Studio Code C# Well Document everyline saying what the code does. Include designer code and .cscode Extra 6-1 Create a simple calculator In this exercise, you'l1 create a form that accepts two operands and an operator from the user and then performs the requested operation. Simple Calculator Operand 1: 86 Operator Operand 2 11.11 Resut 7.7408 1. Start a new project named SimpleCalculator in the Extra Exercises Chapter 06SimpleCalculator directory 2. Add labels, text boxes, and buttons to the...

  • Project 2 Description Create a Visual C# project that when an employee's biweekly sales amount is...

    Project 2 Description Create a Visual C# project that when an employee's biweekly sales amount is entered and the Calculate button is pressed, the gross pay, deductions, and net pay will be displayed. Each employee will receive a base pay of $1200 plus a sales commission of 8% of sales. Once the net pay has been calculated, display the budget amount for each category listed below based on the percentages given.         base pay = $1200; use a named...

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