Solved

Visual Basic 2008

Posted on 2010-11-23
8
263 Views
Last Modified: 2012-05-10
Here is the code I came up with in order to find the AVG population between 3 cities it had to be from a text file, can somebody help: Basically I don't know how to enter AVG = population 3 cities / 3  the population is in thousands and the AVG I guess has to be in %

                 
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
        Dim sr As IO.StreamReader = IO.File.OpenText("Population.txt")
        'The file Population.Txt is in the Debug Subfolder of the bin subfolder of the folder
        Dim City As String
        Dim Population As Double 'Population of each city         
         Dim Total As Double 'Total population of the three cities
        Dim Average As Double 'Average population between the three cities
        Dim fmtStr As String = "{1,12}{1,6:N3}"
        city = sr.ReadLine
        Total += CDbl(sr.ReadLine) 'Increment total by the Population associated with the city
        city = sr.ReadLine
        Total += CDbl(sr.ReadLine)
        city = sr.ReadLine
        Total += CDbl(sr.ReadLine)
        sr.Close()
        sr = IO.File.OpenText("TextPopulation.txt") 'open the file anew
        city = sr.ReadLine
        Population = CDbl(sr.ReadLine)
        LstAverage.Items.Add(String.Format(fmtStr, city, Population / Total))
      city = sr.ReadLine
        Population = CDbl(sr.ReadLine)
        LstAverage.Items.Add(String.Format(fmtStr,city, Population / Total))
       city = sr.ReadLine
        Population = CDbl(sr.ReadLine)
        LstAverage.Items.Add(String.Format(fmtStr, city, Population / Total))
        sr.Close()
End Sub

Open in new window

0
Comment
Question by:nouvelle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 17

Expert Comment

by:Zhaolai
ID: 34202795
It seems like you are calculating percentage instead of average. To get average, add the following line after line 14 in your code:
Average = Total / 3
0
 

Author Comment

by:nouvelle
ID: 34202811
Thanks for your answer.
It did not work. I want this code to display the average between the 3 cities. Any ideas?
0
 

Author Comment

by:nouvelle
ID: 34202830
How can I tell it:

D= A+B+C
D= D/3
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:Zhaolai
ID: 34202838
There is one average in this case. It is Average = Total /3.
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 34202849
You can display the average in a label on the form, like this:

Label1.Text = "Average population: " & Average
0
 

Author Comment

by:nouvelle
ID: 34202877
Yes one average that is the total of the 3 populations divided by the number of cities in this case 3.
I am sorry to ask this but I am in the early stages of learning VB
if I do:  Label1.Text = "Average population: " & Average
will that still be considered that my work on this code is based on the textfile? because that's part of the requirement.
Thanks
0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
ID: 34202914
Yes, the original data is from the text file.
To summarize my earlier comments, add a label to your form and name it lblAverage. Then the code:
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
        Dim sr As IO.StreamReader = IO.File.OpenText("Population.txt")
        'The file Population.Txt is in the Debug Subfolder of the bin subfolder of the folder
        Dim City As String
        Dim Population As Double 'Population of each city         
         Dim Total As Double 'Total population of the three cities
        Dim Average As Double 'Average population between the three cities
        Dim fmtStr As String = "{1,12}{1,6:N3}"
        city = sr.ReadLine
        Total += CDbl(sr.ReadLine) 'Increment total by the Population associated with the city
        city = sr.ReadLine
        Total += CDbl(sr.ReadLine)
        city = sr.ReadLine
        Total += CDbl(sr.ReadLine)
        sr.Close()
        Average = Total /3
        lblAverage.Text = "Average population: " & Average.ToString("#.##")
        sr = IO.File.OpenText("TextPopulation.txt") 'open the file anew
        city = sr.ReadLine
        Population = CDbl(sr.ReadLine)
        LstAverage.Items.Add(String.Format(fmtStr, city, Population / Total))
      city = sr.ReadLine
        Population = CDbl(sr.ReadLine)
        LstAverage.Items.Add(String.Format(fmtStr,city, Population / Total))
       city = sr.ReadLine
        Population = CDbl(sr.ReadLine)
        LstAverage.Items.Add(String.Format(fmtStr, city, Population / Total))
        sr.Close()
End Sub

Open in new window

0
 

Author Comment

by:nouvelle
ID: 34202972
Thanks a lot!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net creating Contact in Outlook 1 53
Groupbox Control ? 2 27
Formula in  Excel or vba to transfer values from Column X to Y 5 43
need assistance with a VBscript 3 37
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question