Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,561 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,905 people online right now. Registration is fast and FREE... Join Now!




Calculate Max, min, aver and total

 
Reply to this topicStart new topic

Calculate Max, min, aver and total, The output of my calculation are always 0

ontimebrown
14 May, 2008 - 03:22 AM
Post #1

New D.I.C Head
*

Joined: 14 May, 2008
Posts: 2

Hello, I am working on a rainfall calculator. I keep getting the zeros for the totals (Minimum, Maximum, Average and Total Rainfall output). Everything else works as expected. Can you please help me with this?

vb

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub btnInputMonthlyRainfall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInputMonthlyRainfall.Click
' Calculate and display monthly rainfall input
' Reference??
Dim intCount As Integer ' loop counter
Dim intMonths(11) As Integer
Dim strMonths() As String = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
Dim intRainfall As Integer ' Rainfall amount

lstRainfall.Items.Add("Monthly Rainfall Input")
lstRainfall.Items.Add("______________________")

'Rainfall for each month
For intCount = 0 To 11
intRainfall = (InputBox("Enter the amount of rainfall in inches for" & " " & strMonths(intCount)))
lstRainfall.Items.Add(strMonths(intCount) & " " & (intRainfall).ToString())
intRainfall = intMonths(11)
Next intCount
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

Private Sub btnDisplayStats_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayStats.Click

' Display Stats
Dim intCount As Integer
Dim intMonths(11) As Integer
Dim strMonths() As String = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
Dim intMaximum As Integer
Dim intMinimum As Integer
Dim dblAverage As Double
Dim intTotal As Integer = 0

'calculate maximum
For intCount = 1 To (intMonths.Length - 1)
If intMonths(intCount) > intMaximum Then
intMaximum = intMonths(intCount)
End If
Next intCount

'calculate(minimum)
For intCount = 1 To (strMonths.Length - 1)
If intMonths(intCount) < intMinimum Then
intMinimum = strMonths(intCount)
End If
Next intCount

'calculate average
For intCount = 0 To (strMonths.Length - 1)
intTotal += intMonths(intCount)
Next intCount
'use floating-point division to compute the average
dblAverage = (intTotal / strMonths.Length)

'calculate total rainfall
For intCount = 0 To (strMonths.Length - 1)
intTotal += intMonths(intCount)
Next intCount

'list output for stats
lblAnnualRainfall.Text = ("The total annual rainfall was " & intTotal.ToString())
lblAverageRainfall.Text = ("The average monthly rainfall was " & dblAverage.ToString())
lblMinimumRainfall.Text = ("The minimum monthly rainfall was " & intMinimum.ToString())
lblMaximumRainfall.Text = ("The maximum monthly rainfall was " & intMaximum.ToString())


lblAnnualRainfall.Visible = True
lblAverageRainfall.Visible = True
lblMinimumRainfall.Visible = True
lblMaximumRainfall.Visible = True

End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

'Clear all display fields
lblAnnualRainfall.Visible = False
lblAverageRainfall.Visible = False
lblMinimumRainfall.Visible = False
lblMaximumRainfall.Visible = False
End Sub
End Class


EDIT: Code blocks added, please use them in future posts => code.gif

This post has been edited by PsychoCoder: 14 May, 2008 - 04:59 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Calculate Max, Min, Aver And Total
14 May, 2008 - 05:00 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to VB.NET (Will look through the code when I get a chance smile.gif )
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Calculate Max, Min, Aver And Total
14 May, 2008 - 08:23 AM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,947



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You never assign any values to your intMonths(11) array inside the btnDisplayStats_Click event.

So as a result all of your calculations will result in 0. You either need to make the array a class level array, so it will retain its values. Or you need to repopulate the array inside the btnDisplayStats_Click event.
User is offlineProfile CardPM
+Quote Post

ontimebrown
RE: Calculate Max, Min, Aver And Total
14 May, 2008 - 03:29 PM
Post #4

New D.I.C Head
*

Joined: 14 May, 2008
Posts: 2

I can't figure out how to get the values from the btnInputMonthlyRainfall_Click to link to the btnDisplayStats_Click Can you please give me another example.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:39PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month