0

I have two arrays: TempSubject(), which carries a list of subjects, and friRoutine(), which indicates when subjects are taught.

I need to enforce a rule that no particular subject can not be taught twice on a specific day. I have a function named DetectDuplicate(friRoutine, Tempsubject(i)), which checks friRoutine() with TempSubject() and finds whether friRoutine already contains a particular subject. If No, then add that unique subject to friRoutine. Otherwise continue through the loop for next subject. My code is furnished below as follows-

Dim TempSubject() As String = {"Maths","English","Sanskrit","Sanskrit","Urdu","Urdu","Urdu","French","French","French","Physical Education","Physical Education","Game","Game", "Science"}

dim c4 as integer = 0
dim limitcounter = 0
dim friRoutine() as string
reedim friRoutine(6)

While c4 < TempSubject.Length
    If DetectDuplicate(friRoutine, TempSubject(c4)) = False And limitcounter4 < 7 Then
        friRoutine(limitcounter4) = TempSubject(c4)
        'MessageBox.Show(c1 & ": " & limitcounter1 & ": " & tueRoutine(limitcounter1))
        Debug.WriteLine("Friday: " & c4 & ": " & limitcounter4 & ": " & friRoutine(limitcounter4))
        TempSubject.RemoveAt(c4)
        limitcounter4 = limitcounter4 + 1
    End If
    c4 = c4 + 1
End While


Private Function DetectDuplicate(ByVal arr As Array, ByVal str As String) As Boolean
    Dim numcount As Integer = 0
    For numcount = 0 To arr.Length - 1
        If arr(numcount) = str Then
            Return True
        End If
    Next
    Return False
End Function

The Output is supposed to be

friRoutine = Math, english, sanskrit, Urdu, French, Physical education, game

but unfortunately, English is missing. the output is:

friRoutine = Math, sanskrit, Urdu, French, Physical education, game, science

I'm afraid of using my code as it may spoil the entire process at any point of time. I think the problem is with Function DetectDuplicate()

Edit (From a comment)

I have to remove the used elements. My original program will have 42 subjects and 6 days. 7 periods everyday. For example if i picked up 7 unique subjects for Monday then for the next 6 days i must have only 35 subjects to be adjusted for rest of the 5 days.

2
  • I'm sure we saw this question in the past couple of weeks, but I can't find it anywhere. Did you ask it before and delete it? Commented Feb 26, 2020 at 2:41
  • Can you just post the homework question here so we can understand the requirement? Commented Feb 26, 2020 at 2:42

2 Answers 2

3

The first thing I would do is convert the arrays to generic Lists. So this:

Dim TempSubject() As String = {"Maths","English","Sanskrit","Sanskrit","Urdu","Urdu","Urdu","French","French","French","Physical Education","Physical Education","Game","Game", "Science"}
Dim friRoutine() As String

Becomes this:

Dim TempSubject As New List(Of String) From {"Maths","English","Sanskrit","Sanskrit","Urdu","Urdu","Urdu","French","French","French","Physical Education","Physical Education","Game","Game", "Science"}
Dim friRoutine As New List(Of String)(6)

You can access items in these lists by index, just like with arrays, but now it becomes MUCH easier to do things like add or remove entries. At least, I would do this for the friRoutine collection; there is some argument to made for an array in way TempSubject is used in this code, but even there the data likely came form somewhere that would justify a List(Of String) instead.

That done, you can greatly simplify the code:

friRoutine.AddRange(TempSubject)
friRoutine = friRoutine.Distinct().Take(7).ToList()

Or, to show the entire sample:

Dim TempSubject = {"Maths","English","Sanskrit","Sanskrit","Urdu","Urdu","Urdu","French","French","French","Physical Education","Physical Education","Game","Game", "Science"}

Dim friRoutine As New List(Of String)(TempSubject.Distinct().Take(7))
For i As Integer = 0 to friRoutine.Count - 1
    Debug.WriteLine($"Friday: {i} : {friRoutine(i)}")
Next 

And now that we see the data for friRoutine can be computed from a single line of code, one wonders if we really need the separate collection at all.

As for the original code, the big problem was this line:

TempSubject.RemoveAt(c4)

This modified the collection while still looping through it. Everything would shift forward, causing some items to be skipped.

Sign up to request clarification or add additional context in comments.

1 Comment

Props for picking up on the limit of 7 items
0

Just use Enumerable.Distinct<T>()

Dim TempSubject = {"Maths","English","Sanskrit","Sanskrit","Urdu","Urdu","Urdu","French","French","French","Physical Education","Physical Education","Game","Game", "Science"}

Dim friRoutine = TempSubject.Distinct().ToArray()

If you want a List output instead, then it's simply

Dim friRoutine = TempSubject.Distinct().ToList()

Since both array and List are Enumerable, TempSubject can be either an array or List.

1 Comment

@Joel Coehoorn But i have to remove the used elements. My original program will have 42 subjects and 6 days. 7 periods everyday. For example if i picked up 7 unique subjects for Monday then for the next 6 days i must have only 35 subjects to be adjusted for rest of the 5 days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.