SantiARC
SantiARC

Reputation: 13

Search in a two-dimensional array

I'm trying to find the values in different points of the array. When I run the code, it always goes to The value doesn't exists, also I do not know how to count the values that are same r.

    r = 0 
    c = txtbbus.Text
    For i = 0 To n - 1
        For j = 0 To n - 1
            If a(i, j) = c Then
                txtbres.Text = "The value exists " & r & " and it's in the position (" & i & ", " & j & ") "
            Else
                txtbres.Text = "The value doesn't exists"
            End If
        Next j
    Next i

And this is how I initialize a:

    txtbmatriz.Text = ""
    For i = 0 To n - 1
        For j = 0 To n - 1
            a(i, j) = CInt((100 * Rnd()) + 1)
            txtbmatriz.Text += a(i, j) & " "
            m += a(i, j)
            l += 1
        Next j
        txtbmatriz.Text += vbCrLf
    Next i

Upvotes: 1

Views: 1146

Answers (3)

Jonathan Barraone
Jonathan Barraone

Reputation: 133

Try this:

    r = 0
    c = txtbbus.Text
    Dim i As Integer
    Dim j As Integer
    Dim FoundMatch As Boolean = False

    For i = 0 To n - 1
        For j = 0 To n - 1
            If a(i, j) = c Then
                FoundMatch = True

                Exit For
            End If
        Next j
        If FoundMatch = True Then
            Exit For
        End If
    Next i
    If FoundMatch = True Then
        txtbres.Text = "The value exists " & r & " and it's in the position (" & i & ", " & j & ") "
    Else
        txtbres.Text = "The value doesn't exists"
    End If

Upvotes: 1

user17922293
user17922293

Reputation:

The problem is almost certainly that you don't break out of the loop when you find a match. Your code will only ever show you the result of the last element in the array because you always keep searching to the last element. Once you find a match, there's no point to looking further and, in fact, doing so is detrimental. Once you find a match, stop looking.

Finding a single/first match:

Dim rng As New Random
Dim matrix = New Integer(9, 9) {}

For i = 0 To matrix.GetUpperBound(0)
    For j = 0 To matrix.GetUpperBound(1)
        matrix(i, j) = rng.Next(1, 101)
    Next
Next

Dim target = rng.Next(1, 101)
Dim message As String

For i = 0 To matrix.GetUpperBound(0)
    For j = 0 To matrix.GetUpperBound(1)
        If matrix(i, j) = target Then
            message = $"{target} found at ({i},{j})"
            Exit For
        End If
    Next

    If message IsNot Nothing Then
        Exit For
    End If
Next

Console.WriteLine(If(message, $"{target} not found"))

Finding all matches:

Dim rng As New Random
Dim matrix = New Integer(9, 9) {}

For i = 0 To matrix.GetUpperBound(0)
    For j = 0 To matrix.GetUpperBound(1)
        matrix(i, j) = rng.Next(1, 101)
    Next
Next

Dim target = rng.Next(1, 101)
Dim matches As New List(Of String)

For i = 0 To matrix.GetUpperBound(0)
    For j = 0 To matrix.GetUpperBound(1)
        If matrix(i, j) = target Then
            matches.Add($"({i},{j})")
        End If
    Next
Next

Console.WriteLine(If(matches.Any(),
                     $"{target} found at {String.Join(", ", matches)}",
                     $"{target} not found"))

Upvotes: 2

jbukuts
jbukuts

Reputation: 1

I'm going to assume c = txtbbus.Text is from some form input. Meaning a string. For the equality check you'd be testing against an Int type. Try casting the input from txtbbus.Text as an integer. Also, like the other poster said breaking from the loop on finding your match would also be a good decision.

Upvotes: 0

Related Questions