``````      'Hi all. I'm trying to create an application that computes the average monthly             'rainfall, number of months with above average rainfall, and the number of with           'below average rainfall, given the rainfall amounts for each month in a year.             'is what I have so far.

Sub Main()
Dim sngRainfall() As Single = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
Dim intTotalRainfall As Integer = 0

For K = 1 To 12
Console.WriteLine("Enter Rainfall for month " & K & ":")
intTotalRainfall += sngRainfall(K)
Next

'Calculate average rainfall with the formula: average = total / 12

Compute AvgRainfall as TotalRainfall divided by 12

‘Initialize the above/below average counts to zero to reduce miscalculations

Initialize AboveAvgCount and BelowAvgCount to zero

'Using a for loop again compare the average rainfall to each monthly rainfall
'and count the above-rainfall months and below-rainfall months.
'use twelve iterations once again since asked the user for twelve variables.
'must use another loop since average couldn't be calculated when first
'loop was processed.

For K ranging from 1 to 12

'If the current rainfall amount in the loop denoted by the 'k'location in the array
' is greater than the average.

If Rainfall( K) > AvgRainfall then

'Then add one to the above average count.

Increment AboveAvgCount by 1

'Otherwise, if the rainfall amount for the current 'k' location in the array
'is less than the average.

else if Rainfall(K) < AvgRainfall then

'Then add one to the below average count.

Increment BelowAvgCount by 1

'Otherwise if the rainfall amount isn't greater or less than the average so...

else

'Just ignore that amount and don't count it towards either count variable.

Continue

'end the if statement after deciding the current value is above or below average

End of if statement

'end our for loop after having traversed all twelve values of rainfall array.

End of For loop

'Display the computed results that were able to calculate including the total, average,
'and above/below average counts to the user for review.

Display TotalRainfall, AvgRainfall, AboveAvgCount, and BelowAvgCount with suitable messages.
``````

## All 6 Replies

Sorry First time posting here and I jacked that all up. I'm trying to do the next step of calculating the average rainfall and I'm kind of lost.

`````` Compute AvgRainfall as TotalRainfall divided by 12
``````

It's not that difficult to figure out that

``````AvgRainfall = TotalRailfall / 12.0
``````

And you might want to make TotalRainfall a non-integer value. Rain doesn't fall in discrete amounts. By the way, you might want to make K run from 0 to 11 instead of 1 to 12.

I'm also going to suggest (unless you have been told otherwise by a teacher/professor) to refrain from prefixing variables with their type. It may be just my personal preference but I find TotalRainfall easier to read (and maiintain) than intTotalRainfall or sngTotalRainfall. The prefix doesn't add to the readability and if you change the type of the variable you would have to change every occurrence of the variable name in all of your code.

Thanks for the advice. The textbook suggest that we prefix variables but the instructor hasn't said either way. But I agree with you on that one. I have already completed my assignment but I'll post it if you would like to go over it.

``````Dim sngRainfall(11) As Single
Dim sngTotalRainfall As Single = 0
Dim sngAvgRainFall As Single
Dim intAboveAvgCount As Integer
Dim intBelowAvgCount As Integer
Dim intMonths As Integer = 11

For K = 0 To 11
Console.WriteLine("Enter Rainfall for month " & K + 1 & ":")
sngTotalRainfall += sngRainfall(K)
Next

sngAvgRainFall = (sngTotalRainfall / (intMonths + 1))

For K = 0 To 11
If sngRainfall(K) > sngAvgRainFall Then
intAboveAvgCount += 1
ElseIf sngRainfall(K) < sngAvgRainFall Then
intBelowAvgCount += 1
End If
Next

Console.WriteLine("Total yearly rainfall amount: " & sngTotalRainfall)

Console.WriteLine("Average monthly rainfall amount: " & sngAvgRainFall)

Console.WriteLine("Number of months with above average rainfall: " & intAboveAvgCount)

Console.WriteLine("Number of months with below average rainfall: " & intBelowAvgCount)

Console.WriteLine()

Console.WriteLine("Enter any value to close window")

``````

I'm going to be a little picky here (and some of this is just personal style)...

You set intMonths to 11 instead of 12 and the only place you use it is when you use `intMonths+1`. I suggest you just set it to 12 otherwise the name should be intMonthsMinusOne.

And I think you could replace the last line with

``````Console.ReadLine()
``````

You should never use a variable for something other than what it was intended for. Here you are just using it for a throwaway value. For that matter, since it has a value that should never change it should be declared as a constant.

And, of course, you should add a header (comments) that describe what the program does (but not how it does it) and a few other comments such as

``````'get monthly rainfall from user and calculate total and average amounts
``````

And this may be overkill for a small program but it's best to get into the habit - I add an inline comment whever I declare a variable.

``````Dim intAboveAvgCount As Integer    '# of months above average rainfall
Dim intBelowAvgCount As Integer    '# of months below average rainfall
``````

I was a marker when I was in university and a well commented program always got the extra marks.

Thanks for giving it a go over. I actually forgot to add the comments when I submitted the assignment. Hope he doesn’t knock me for it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.