8 Replies - 9352 Views - Last Post: 09 March 2010 - 12:15 AM Rate Topic: -----

#1 ThinkingDreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-October 06

arrays and value help

Post icon  Posted 06 October 2006 - 11:34 AM

Public Class Form1
	'Paul Cote Jr
	'Advanced VB
	'Fall 2006
	'Page 549 Rainfall Statistics

	Dim strMonths() As String = {"January", "February", "March", "April", _
	"May", "June", "July", "August", "September", "October", "November", _
	"December"}
	Dim intMonths(11) As Integer
	Dim intRain() As Integer





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

	End Sub

	Private Sub btnRainfall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRainfall.Click
		'calculate and display monthly rainfall
		'Dim intMonths(11) As Integer
		Dim intCount As Integer 'loop counter
		Dim intRain As Integer 'amount of rain
		'Dim intMonth(11)

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

		'get the rain for each month
		For intCount = 0 To 11
			intRain = (InputBox("Enter the inches of rainfall for" & " " & strMonths(intCount)))
			lstRainfall.Items.Add(strMonths(intCount) & " " & (intRain).ToString())
			intRain = intMonths(11)
		Next intCount

	End Sub

	Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
		lstRainfall.Items.Clear()
		lblMonthly.Visible = False
		lblAnnual.Visible = False
		lblMinimum.Visible = False
		lblMaximum.Visible = False
		lbl1.Visible = False

	End Sub

	Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
		Dim intCount As Integer
		Dim intMaximum As Integer
		Dim intMinimum As Integer = intMonths(0)
		Dim dblAverage As Double
		Dim intTotal As Integer = 0

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

		'calculate(minimum)
		For intCount = 0 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

		lblMonthly.Visible = True
		lblAnnual.Visible = True
		lblMinimum.Visible = True
		lblMaximum.Visible = True
		lbl1.Visible = True

		lblMonthly.Text = "The average monthly rainfall was " & dblAverage
		lblAnnual.Text = "The total annual rainfall was " & intTotal
		'		lblMinimum.Text = "The minimum monthly rainfall was " & intMinimum
		lblMaximum.Text = "The maximum monthly rainfall was " & intMaximum

	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		lblMonthly.Visible = False
		lblAnnual.Visible = False
		lblMinimum.Visible = False
		lblMaximum.Visible = False
		lbl1.Visible = False
	End Sub
End Class



I am not getting anything for the values for max, min and average. Isn't the rain inputted in intRain? Could someone clue me in on arrays a bit, so I understand whats going on in the equation. thanks. Paul

Is This A Good Question/Topic? 0
  • +

Replies To: arrays and value help

#2 VisualBasicBeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 04-March 07

Re: arrays and value help

Posted 31 March 2007 - 03:29 PM

Hello,
I am also trying to figure this problem out. I made my Total, Max, Min and Average all functions. The problem im having is that when I try and add the items to the listbox, IM getting 0! Could someone please assist me in getting the items to add to the box? I made a function for my Display stats button and it is referred to as p_intRainfallstats(). I'm not sure if I need an input function?? Thanks
Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
        Dim intMonths(11) As Integer
        Dim intCount As Integer
        Dim strMonths() As String = {"January", "February", "March", "April", "May", _
           "June", "July", "August", "September", "October", "November", "December"}

        For intCount = 0 To 11
            intMonths(intCount) = CInt(InputBox("Enter the inches of rainfall for" & " " & strMonths(intCount)))
            lstMonthlyRainfall.Items.Add(p_intRainfallStats(intCount) & " for " & p_strMonths(intCount))
            p_intRainfallStats = intMonths(11)
        Next intCount
    End Sub

Was This Post Helpful? 0
  • +
  • -

#3 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: arrays and value help

Posted 31 March 2007 - 06:05 PM

Hi to both of you,

First, your question on arrays.

Arrays in VB can be Zero or One based, meaning the first element is indexed at 0 or 1. This is controlled by the use of OPTION BASE X (where X is 0 or 1), and is globlally applied to all your arrays.

I notice that your code appears to be using OPTION BASE 0, yet some of your indexing appears to be incorrect.

First, the number of elements if you are using months must be 12, yet, a couple of the arrays are dimensioned to 11!! The confusion is usually a misunderstanding between Zero based arrays and the actual number of elements.

For Example

El Ind Value
1 0 January
2 1 February
3 2 March

El represents an OPTION BASE 1 array index and Ind is an OPTION BASE 0 array. You may now see that the 0 base last element is El-1, but the count is still 3 independent of the OPTION BASE setting. In one case I can see you have dimensioned the array to 11, and not 12 as you should have.

Back to your other problems...

Did you intend that the SUB's return a value? If so, they must be a FUNCTION, which has a signature like this:
PUBLIC|PRIVATE FUNCTION MyFunction ( ..Input args.. ) AS Type, so now you can actually return values where required.

VB has undergone a number of radical changes, including the use of classes and threaded items. This leads to the idea of scope and accessibility. If you declare the arrays to store data locally to a function, then they are not available outside of the function.

Finally, it is better to have your functions perform one job, such as getting the users input and storing it into the array for the months, but not doing any other processing at that time. The use of this programming style is to reduce the potential for errors because the programmer confused a variable at the time of writing.

So, make sure your rainfall array is globally available to your functions, make each function do one job and use functions that return values to make your programming a little easier. I suggest functions like:
maxRain
minRain
avgRain

would help clarify and improve your code a lot.

good luck
Was This Post Helpful? 0
  • +
  • -

#4 VisualBasicBeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 04-March 07

Re: arrays and value help

Posted 01 April 2007 - 09:52 PM

Thanks for the advice!!! I made some changes and everything is calculating!! :)
Was This Post Helpful? 0
  • +
  • -

#5 SgtJames  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-December 08

Re: arrays and value help

Posted 15 December 2008 - 01:43 AM

OK I finished this problem. The previous post where somewhat helpful although to code led me a little astray. Here is code that worked for me. The only thing that is missing is validation for the input box. I just don't want to mess with it right now so here it is. Hope this helps someone. So if someone else wants to figure that out, great.


Public Class Form1

'Code written by: SgtJames
12/15/2008
For IS51 homework

	'Delcare array and variabels
	Dim strMonths() As String = {"January", "February", "March", "April", _
   "May", "June", "July", "August", "September", "October", "November", _
   "December"}
	Dim intMonths(11) As Integer
	Dim intRain() As Integer



	Public Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click


		Dim intcount As Integer  'Counter
		Dim intRain As Integer  'Amount of raim

		'Display Header for OutPut listbox
		OutPut.Items.Add("Monthly Rainfall Input")
		OutPut.Items.Add("----------------------")

		'Get data from user
		For intcount = 0 To 11

			intRain = CInt((InputBox("Enter the inches of rainfall for " & strMonths(intcount))))
			OutPut.Items.Add(intRain & " for " & strMonths(intcount))
			intMonths(intcount) = intRain
		Next intcount

	

	End Sub
	Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

		FindLargest()
		FindSmallest()
		TotalRainFall()
		Average()

	End Sub

	Public Function FindLargest() As Integer

		Dim intCount As Integer
		'Find Largest
		Dim intMax As Integer = intMonths(0)
		For intCount = 1 To (intMonths.Length - 1)
			If intMonths(intCount) > intMax Then
				intMax = intMonths(intCount)
			End If
		Next intCount
		'Display result
		lblMaximum.Text = "The maximum monthly rainfall was " & intMax.ToString


	End Function

	Public Function FindSmallest() As Integer
		'Find Smallest
		Dim intCount As Integer
		Dim intMin As Integer = intMonths(0)
		For intCount = 1 To (intMonths.Length - 1)
			If intMonths(intCount) < intMin Then
				intMin = intMonths(intCount)
			End If
		Next intCount
		'Display result
		lblMinimum.Text = "The minimum monthly rainfall was " & intMin

	End Function

	Public Function TotalRainFall() As Integer
		'Calculate Total
		Dim intCount As Integer
		Dim intTotal As Integer = 0
		For intCount = 0 To (strMonths.Length - 1)
			intTotal += intMonths(intCount)
		Next intCount

		'Display Result
		lblTotal.Text = "The total annual rainfall was " & intTotal

	End Function

	Public Function Average() As Integer

		'Calculate average
		Dim intCount As Integer
		Dim intTotal As Integer = 0
		Dim intAverage As Integer
		For intCount = 0 To (strMonths.Length - 1)
			intTotal += intMonths(intCount)
			intAverage = CInt(intTotal / 12)
		Next intCount

		'Display Result
		lblAverage.Text = "The average monthly rainfall was " & intAverage.ToString("d")

	End Function

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

		'Close Program
		Me.Close()

	End Sub

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

		'Clear all fields
		OutPut.Items.Clear()
		lblAverage.Text = String.Empty
		lblMaximum.Text = String.Empty
		lblMinimum.Text = String.Empty
		lblTotal.Text = String.Empty




	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#6 t3hd3r3k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-March 09

Re: arrays and value help

Posted 15 March 2009 - 12:10 PM

Sorry to bumb an old topic however i also have this assignment, and i have gotten this far, however i am having a terrible time trying to learn how to validate the data put inside the inputbox. Does anyone know a resource where i could learn how to do this?

This post has been edited by t3hd3r3k: 15 March 2009 - 12:15 PM

Was This Post Helpful? 0
  • +
  • -

#7 Hawkbacker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-December 09

Re: arrays and value help

Posted 09 December 2009 - 08:11 PM

Thanks for these posts. They were helpful. If anyone needs help with the validation part, I have in the code below to check for negative numbers and non-numeric values:

Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click

		' This procedure gathers the monthly rainfall totals 
		' from the user for each month of the year
		' The totals for each month are displayed in the input box.
		' Create a prompting message for the user
		' Create validating event to check for bad data
		' Get and display the rainfall amount by each month

		Dim intcount As Integer		 ' Counter
		Dim decRain As Decimal		  ' Amount of rainfall to be entered

		'Display header for Input listbox

		lstInput.Items.Add("Monthly Rainfall Input")
		lstInput.Items.Add("------------------------")

		' Get the rainfall amount by each month
		' Validate that data is numeric
		For intcount = 0 To 11
			Dim intValidationerrors As Decimal = 1
			Do Until intValidationerrors = 0

				' Set number of validation errors = 0
				intValidationerrors = 0
				Dim strTemp As String = InputBox("Enter the amount of rainfall in inches for " & strMonths(intcount))
				' put error checking here
				If Not IsNumeric(strTemp) Then
					MessageBox.Show("Value must be a number", "Error")
					intValidationerrors = 1
				Else
					decRain = CDec(strTemp)
					If decRain < 0 Then
						MessageBox.Show("Value cannot be a negative number", "Error")
						intValidationerrors = 1
					End If
				End If
			Loop

			decMonths(intcount) = decRain
			lstInput.Items.Add(decMonths(intcount) & " for " & strMonths(intcount))

		Next intcount



________________________
http://www.leavenworthwa.net
Was This Post Helpful? 0
  • +
  • -

#8 Guest_clee*


Reputation:

Re: arrays and value help

Posted 08 March 2010 - 02:03 PM

wow, thank you so much! i also had trouble with the validation.
Was This Post Helpful? 0

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: arrays and value help

Posted 09 March 2010 - 12:15 AM

That is VB.NET code so this is being moved to the VB.NET forum :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1