10 Replies - 299 Views - Last Post: 16 July 2014 - 10:34 AM Rate Topic: -----

#1 stinkytotoro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 31-May 14

Nested Loop Histogram

Posted 15 July 2014 - 06:20 PM

Display a company’s sales figures for several years in a histogram and calculate the average yearly sales. When the user clicks on the button, the amount of sales (in an integer) for each year should be obtained from the user with input dialog boxes. (Use a nested loop)



 Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim begin, endYear, salesamount, years As Integer
        Dim endamount As Double
        begin = CInt(txtBegin.Text)
        endYear = CInt(txtEnd.Text)
        years = endYear - begin + 1
        lstSales.Items.Add("Sales in Millions")
        lstSales.Items.Add("")
        For i = begin To endYear
            salesamount = CInt(InputBox(" Enter the amount of sales for " & i & " in millions."))
            lstSales.Items.Add(i & " * * * * * * * " & salesamount)
            For K = begin To endYear
                K += salesamount
                endamount = salesamount / years
                txtAvg.Text = CStr(endamount)
            Next
            Next
    End Sub
End Class




I need to figure out how to take the amount of money and make it show up in * beside the number in the list box and I am not sure of how to keep track of the average. I uploaded a picture of what it should look like.
Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Nested Loop Histogram

#2 Kizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-July 14

Re: Nested Loop Histogram

Posted 16 July 2014 - 01:37 AM

Could you post an image of what yours currently looks like?
Was This Post Helpful? 0
  • +
  • -

#3 stinkytotoro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 31-May 14

Re: Nested Loop Histogram

Posted 16 July 2014 - 08:59 AM

View PostKizzle, on 16 July 2014 - 01:37 AM, said:

Could you post an image of what yours currently looks like?

Attached Image

This is it, I am stuck with the average and about how to get the large dots for the histogram
Was This Post Helpful? 0
  • +
  • -

#4 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,119
  • Joined: 11-May 12

Re: Nested Loop Histogram

Posted 16 July 2014 - 09:22 AM

well how are you getting the amount that is in millions? After you've figured out the amount, you could use the .PadRight() and use one of it's overloads to specify the padding character. So the way I would do it it is by breaking it into manageable steps.

1. Enter in a beginning year
2. Enter an ending year
3. click the gather and display data button
3a. calculate the number of years the user wants by taking the ending year - beginning year.
3b. create a loop that loops the above number of times
3b1. within that loop show an input box that says to enter the number in millions
3b2. take that number, convert it into a string, then use .PadRight(number, "*") then add that string into your textbox.

for the average you will need to keep a running total of the numbers entered and after the loop above take that total and divide it by the number of years in 3a.
Was This Post Helpful? 1
  • +
  • -

#5 stinkytotoro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 31-May 14

Re: Nested Loop Histogram

Posted 16 July 2014 - 09:45 AM

I am unsure of how to use .PadRight and do not think I am supposed to use it for this question, I have the hint that * -> Chr(149) so I think I need to figure it out that way.
For the average do you have any hints of how to keep a running total from the list box? I have this so far but it just keeps putting out the last number I entered.
 Dim begin, endYear, salesamount, years, total, average As Integer
        begin = CInt(txtBegin.Text)
        endYear = CInt(txtEnd.Text)
        years = endYear - begin + 1
        lstSales.Items.Add("Sales in Millions")
        lstSales.Items.Add("")
        For i = begin To endYear
            salesamount = CInt(InputBox(" Enter the amount of sales for " & i & " in millions."))
            total = salesamount 
            lstSales.Items.Add(i & " * * * * * " & salesamount)
        Next
        average = CInt(total / years)
        txtAvg.Text = CStr(average.ToString("C")) & "Million"


This post has been edited by andrewsw: 16 July 2014 - 10:22 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#6 stinkytotoro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 31-May 14

Re: Nested Loop Histogram

Posted 16 July 2014 - 09:57 AM

Option Strict On
Public Class frmHistogram
    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim begin, endYear, salesamount, years, total As Integer
        Dim average As Double
        begin = CInt(txtBegin.Text)
        endYear = CInt(txtEnd.Text)
        years = endYear - begin + 1
        lstSales.Items.Add("Sales in Millions")
        lstSales.Items.Add("")
        For i = begin To endYear
            salesamount = CInt(InputBox(" Enter the amount of sales for " & i & " in millions."))
            lstSales.Items.Add(i & " * * * * * " & salesamount)
            total += salesamount
        Next
        average = total / years
        txtAvg.Text = CStr(average.ToString("C")) & " Million"
    End Sub



Average is now working, easy solution sometimes my brain gets all fired up though.
Now to figure out the Chr(149) Histogram :whatsthat:
Was This Post Helpful? 0
  • +
  • -

#7 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,119
  • Joined: 11-May 12

Re: Nested Loop Histogram

Posted 16 July 2014 - 10:03 AM

If you aren't allowed to use .PadRight, which is the easiest solution, you will need to create a second loop that will concatenate to a string and loop through it equal to the number entered by the user. something like
Dim tempStr = ""
for i as integer = 0 to salesAmount
  tempString &= Chr(149).ToString()
next

where you have lstSales.Items.Add(i...)...
then instead of just having " * * * * * " & salesAmount you can use
lstSales.Items.Add(i & tempString & salesAmount)

This post has been edited by ybadragon: 16 July 2014 - 10:06 AM

Was This Post Helpful? 1
  • +
  • -

#8 stinkytotoro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 31-May 14

Re: Nested Loop Histogram

Posted 16 July 2014 - 10:17 AM

View Postybadragon, on 16 July 2014 - 10:03 AM, said:

If you aren't allowed to use .PadRight, which is the easiest solution, you will need to create a second loop that will concatenate to a string and loop through it equal to the number entered by the user. something like
Dim tempStr = ""
for i as integer = 0 to salesAmount
  tempString &= Chr(149).ToString()
next

where you have lstSales.Items.Add(i...)...
then instead of just having " * * * * * " & salesAmount you can use
lstSales.Items.Add(i & tempString & salesAmount)



  For i = begin To endYear 'loop for entering sales for each year 
            'input dialog to enter sales for years
            salesamount = CInt(InputBox(" Enter the amount of sales for " & i & " in millions."))
            For k As Integer = 0 To salesamount
                str &= Chr(149)
            Next
            total += salesamount 'keeps running total
            lstSales.Items.Add(i & str & salesamount)
        Next
        average = total / years
        txtAvg.Text = CStr(average.ToString("C")) & " Million"



This works but after the first sales is inputted the second ones double the amount of "*"

Attached Image
this is what it looks like
Was This Post Helpful? 0
  • +
  • -

#9 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,119
  • Joined: 11-May 12

Re: Nested Loop Histogram

Posted 16 July 2014 - 10:18 AM

yep, you need to clear str once you add it to the list so str = ""
Was This Post Helpful? 1
  • +
  • -

#10 stinkytotoro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 31-May 14

Re: Nested Loop Histogram

Posted 16 July 2014 - 10:26 AM

:angel: Thank you
Was This Post Helpful? 0
  • +
  • -

#11 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,119
  • Joined: 11-May 12

Re: Nested Loop Histogram

Posted 16 July 2014 - 10:34 AM

no problem, glad i could help :tup:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1