|
-
Aug 14th, 2007, 08:18 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|