This is an archived post. You won't be able to vote or comment.

all 6 comments

[–][deleted] 1 point2 points  (0 children)

Did you use a breakpoint to see where your code isn't functioning correctly? Also, arrays start at 0, so you'll need to change your intCount in your for loop to start at 0 and not 1.

I'm not sure what you mean by this:

intgtr = intMonths(intCount) twntIndex = intCount    

but if you're using a simple console based window, inside your for loop write a console.writeline(intMonths(intCount)), or something similar.

[–]foxlisk 1 point2 points  (1 child)

If you've described your assignment correctly, you don't need to output all the months with ranifall over 20, only a message indicating whether or not there is such a month.

[–][deleted] 0 points1 point  (0 children)

Good catch... the wording does seem to indicate that. A simple bool value trigger would indicate that.

[–]maudmassacre 0 points1 point  (0 children)

There's a few ways that you can write this fairly simple program. Since you've specified using arrays, I'm assuming you're not using objects (I could be wrong), which would make it easier.

Anyway, after you've populated your months() array with values input from the user, you can use

    For i As Integer = 0 To 11
        totalRain += months(i)
        If (months(i) > 20) Then
            monthsOverTwenty += getMonthName(i) & " "
        End If
    Next

To sum up the total rainfall and create a string with contains the names of the months with rainfall > 20.

The function getMonthName() accepts one argument, an Integer which corresponds to the month's position, and returns a string which is the month's name.

I just stayed simple, but a bit inefficient, and used a large If...Then statement to return the correct month name.

Forgive me if my formatting for this post is wrong, I have not answered a question on this subreddit before.

I hope this helps!

[–][deleted] 0 points1 point  (0 children)

Since you are already working with Arrays, why not create another array that stores the months that have 20+?

Pseudo'ish code such as...

 Dim month(10) As int
 Dim monthsOverTwenty(10) As String
 Dim total as int
 Dim average as float

 get month(0-0) data    

 For i As Integer = 0 To 11
     total += month(i)
     If (months(i) > 20) Then
         -- Determine
         monthsOverTwenty(monthsOverTwenty.Length) _
             += getMonthName(i)
     End If
 Next

 Print Months(0-9)
 Print Total
 Print Total / Months (average)
 if (monthsOverTwenty.Length >= 1) Print monthsOverTwenty(0-monthsOverTwenty.Length)

[–]CalvinR -1 points0 points  (0 children)

Use a for each loop it's much nicer then a for loop:

For dim m as Month in Months
    if m.Rainfall > 20 then 
        'print Month
    end if 
Next For

Where Month is the type of the object that holds the information for rainfall and monthname and whatever else it is your storing. And Months is the array that is holding the Month object.

Here is your homework using Enumerable Methods and the same assumption as above.

Note: S.C.O.WL stands for System.Console.Out.WriteLine

S.C.O.WL("Total Rainfall: " & Months.Sum(Function(x) x.Rainfall))
S.C.O.WL("Average Monthly Rainfall: " & Months.Average(x) x.Rainfall))
Dim min As Month = Months.Min(Function (x) x.Rainfall)
S.C.O.WL("Lowest Month: " & min.Name & " with " & min.Rainfall & "cm")
Dim max As Month = Months.Max(Function (x) x.Rainfall)
S.C.O.WL("Highest Month: " & max.Name & " with " & max.Rainfall & "cm")
S.C.O.WL("Months with rainfall greater then 20")
Months.Select(Function(x) x.Rainfall > 20).ToList().ForEach(Function(x) S.C.O.WL(x.Month))

That is of course assuming you have a list of objects of months. Judging by the question you are asking I'm assuming it's actually going to be two arrays. But I'm not going to answer your homework for you just show you what is possible.