Nested Loop Histogram

Page 1 of 1

10 Replies - 2527 Views - Last Post: 16 July 2014 - 10:34 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=350426&amp;s=fabd277b42c9e99eafb93829d35815ca&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 stinkytotoro

Reputation: 0
• Posts: 53
• 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
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.

Is This A Good Question/Topic? 0

Replies To: Nested Loop Histogram

#2 Kizzle

Reputation: 4
• Posts: 26
• 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?

#3 stinkytotoro

Reputation: 0
• Posts: 53
• Joined: 31-May 14

Re: Nested Loop Histogram

Posted 16 July 2014 - 08:59 AM

Kizzle, on 16 July 2014 - 01:37 AM, said:

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

This is it, I am stuck with the average and about how to get the large dots for the histogram

• Home Owner

Reputation: 571
• Posts: 2,647
• 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.

#5 stinkytotoro

Reputation: 0
• Posts: 53
• 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
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

#6 stinkytotoro

Reputation: 0
• Posts: 53
• 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
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

• Home Owner

Reputation: 571
• Posts: 2,647
• 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

```
then instead of just having " * * * * * " & salesAmount you can use

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

#8 stinkytotoro

Reputation: 0
• Posts: 53
• Joined: 31-May 14

Re: Nested Loop Histogram

Posted 16 July 2014 - 10:17 AM

ybadragon, 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

```
then instead of just having " * * * * * " & salesAmount you can use

```  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
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 "*"

this is what it looks like

• Home Owner

Reputation: 571
• Posts: 2,647
• 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 = ""

#10 stinkytotoro

Reputation: 0
• Posts: 53
• Joined: 31-May 14

Re: Nested Loop Histogram

Posted 16 July 2014 - 10:26 AM

Thank you

• Home Owner

Reputation: 571
• Posts: 2,647
• Joined: 11-May 12

Re: Nested Loop Histogram

Posted 16 July 2014 - 10:34 AM

no problem, glad i could help