Results 1 to 3 of 3

Thread: [RESOLVED] [02/03] Breaking down a mathematical answer

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Resolved [RESOLVED] [02/03] Breaking down a mathematical answer

    Hello guys!
    This should be a breeze for all you pros out there.
    I have this sub:
    Code:
        Private Sub suggWeekLimitCalc(ByVal Limit As Integer)
            Dim TempWeeks As Integer
            Dim iIndex As Integer
            Dim avgWeeks As Single
            Dim modWeeks As Integer
    
            avgWeeks = WeekLimit / sugNumSubjects
            modWeeks = WeekLimit Mod sugNumSubjects
    
            ReDim sugSubDurArr(sugNumSubjects)
            For iIndex = 1 To sugNumSubjects
                 sugSubDurArr(iIndex) = WeekLimit -  avgWeeks
             Next iIndex
            MessageBox.Show(avgWeeks.ToString())
    
        End Sub
    Now, what is supposed to happen here is:
    I get a certain amount of weeks, let's presume I get 21 weeks. (This I get somewhere else). This sub (the above sub), is supposed to break down that number of weeks.
    For example, I select 4 subjects, then, I select a duration in months - let's presume I selected 5 months.
    Currently, it returns 21.
    Now, what I'm hoping to achieve with the above sub, is to split the 21 weeks into 4 seperate variables (or elements of an array). The way it would be split would be:
    Array Index 1 = 5
    Array Index 2 = 5
    Array Index 3 = 5
    Array Index 4 = 6
    Which adds up to 21.

    The real problem i'm sitting with is the For Loop. It just keeps on subtratcing the current value of avgWeeks, instead of decrementing avgweeks. This is the results the Loop produces currently:
    Array Index 1 = 16
    Array Index 2 = 16
    Array Index 3 = 16
    Array Index 4 = 16

    And so on. It just keeps subtracting 5 once, and not again.

    Can anyone help me out on this?

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: [02/03] Breaking down a mathematical answer

    Ok, I must go back to school
    This what I ended up doing, which works fine:
    Code:
        Private Sub suggWeekLimitCalc(ByVal Limit As Integer)
            Dim TempWeeks As Integer
            Dim iIndex As Integer
            Dim avgWeeks As Single
            Dim modWeeks As Integer
            Dim divWeek As Integer = WeekLimit
    
            avgWeeks = WeekLimit / sugNumSubjects
            modWeeks = WeekLimit Mod sugNumSubjects
    
            ReDim sugSubDurArr(sugNumSubjects)
            For iIndex = 1 To sugNumSubjects
                divWeek -= avgWeeks
                sugSubDurArr(iIndex) = divWeek
            Next iIndex
            MessageBox.Show(avgWeeks.ToString())
    
        End Sub

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [RESOLVED] [02/03] Breaking down a mathematical answer

    A couple suggestions:
    1. Do not pass in a function or subroutine a parameter that is unneeded (in this case: Limit)
    2. Try not to declare variables that won't be used (in this case: TempWeeks)
    3. If you are to use global variables in a subroutine or function, there's no point of declaring local variables then set them equal to the global variables (in this case: Dim divWeek As Integer = WeekLimit)
    4. Arrays in are zero based index, so if you do "For iIndex = 1 To....", you're skipping the first element in the array.
    5. Last but not least, from what you described in post # 1 and what you ended up doing, the results are not the same.
    In post #1, you had 21 weeks with 4 subjects and you wanted the resulted array to be {5, 5, 5, 6}. However, in post #2 which you said it works fine, the resulted array will be {0, 16, 11, 6, 1}

    And just in case that you still need the result to be as in post # 1, here is a function that will do that for you
    Code:
    Private Function suggWeekLimitCalc(ByVal subjectCount As Integer, _
                                           ByVal totalWeeks As Integer) As Integer()
            If subjectCount < 1 Then
                Throw New ArgumentException("subjectCount can not be less than 1")
            End If
            Dim retArray(subjectCount - 1) As Integer
            Dim value As Integer = totalWeeks \ subjectCount
            Dim extra As Integer = totalWeeks Mod subjectCount
            For i As Integer = 0 To subjectCount - 1
                If i = (subjectCount - 1) Then
                    retArray(i) = value + extra
                Else
                    retArray(i) = value
                End If
            Next
            Return (retArray)
        End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width