Mirrored 2d Array

jeneal

Member
Joined
Nov 22, 2007
Messages
8
Programming Experience
Beginner
I need help creating a 2d array that holds a total on 49 values. 7 rows with 7 columns. in position 0,0; 1,1; 2,2; ect... the value is 0. From there 0,1 and 1,0; 0,2 and 2,0; 1,2 and 2,1; ect... are the same. Random numbers are to be inputted into the array. I'm kind of lost in how to do it. I know its using a for loop, but not sure how to write it. Help please.
 
To iterate over a 2D array you would use nested loops:
VB.NET:
For x As Integer = 0 To myArray.GetUpperBound(0) Step 1
    For y As Integer = 0 To myArray.GetUpperBound(1) Step 1
        'Access myArray(x, y) here.
    Next y
Next x
 
I just read your post more carefully:
VB.NET:
For x As Integer = 0 To myArray.GetUpperBound(0) Step 1
    For y As Integer = x To myArray.GetUpperBound(1) Step 1
        'Access myArray(x, y) and myArray(y, x) here.
    Next y
Next x
 
Experiment continues

Okay, this is what I have. I've added the numbers into a 2d array being assigned randomly, but giving the mirrored effect, and setting 0s as the doubles. but nothing pulls into the final text box. Anyone have any suggestions?
VB.NET:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cities() As String = {"Canton", "Cincinnati", "Cleveland", "Columbus", "Dayton", "Toledo", "Youngstown"}
        Dim city1 As String = TextBox1.Text
        Dim city2 As String = TextBox2.Text

        Dim cityVal As Integer
        Dim xVal As Integer = 0
        Dim yVal As Integer = 0
        Dim mi As Integer
        Dim start As Integer = 0

        Dim miles(7, 7) As Integer
        Dim randomGenerator As New Random

        For x As Integer = start To miles.GetUpperBound(0) Step x
            For y As Integer = 0 To miles.GetUpperBound(1) Step y
                mi = randomGenerator.Next(1, 1000)
                miles(x, y) = mi
                miles(y, x) = mi
            Next y
        Next x

        For z As Integer = 0 To z <= 6 Step z
            miles(z, z) = 0
        Next

        cityVal = 1

        While cityVal <> 0
            For check1 As Integer = 0 To check1 <= 6 Step check1
                cityVal = city1.CompareTo(cities(check1))
                If cityVal = 0 Then
                    xVal = check1
                    check1 = 7
                Else
                    MsgBox("Invalid City!", MsgBoxStyle.Exclamation)
                End If
            Next
        End While

        cityVal = 1

        While cityVal <> 0
            For check2 As Integer = 0 To check2 <= 6 Step check2
                cityVal = city2.CompareTo(cities(check2))
                If cityVal = 0 Then
                    yVal = check2
                    check2 = 7
                Else
                    MsgBox("Invalid City!", MsgBoxStyle.Exclamation)
                End If
            Next
        End While

        Dim distance As Integer
        distance = miles(xVal, yVal)
        TextBox3.Text = distance

    End Sub

End Class
 
Last edited by a moderator:
Place a breakpoint at the top of the code and step through it one line at a time with the F10 key. At each line you test the value of each variable to make sure that it's the value you expect, plus you can make sure that execution follows the path you expect.
 
breakpoint

I've entered the breakpoint and went through the code. It is seemingly getting caught in the internal For loop I don't know how to get it out.
 
At each step you need to ask yourself "what do i expect to happen next?". You then press F10 and see if what you expected to happen actually did happen. If not then you know you've found an issue. You absolutely MUST know EXACTLY what you expect to happen at each line of code. It can't possibly do what you want if you don't even know what you want.

Show us the code, tell us what you expect to happen and what actually does happen.
 
code

I was asked to create a program that would take two cities put into text boxes, compair them with a declared string array.

Also a 2d array will be created and filled with random numbers. The array is a mirror of itself such that position 1,2 and 2,1 are the same, the same concept repeating through the array, where variable x = y the result of the 2darray is 0.

The program loops through and checks if the city is vaild and stores it position in the array such that city1 = x and city2 = y. If the city is not found in the array an error box is displayed. When both cities are valid, a random number shows up in the third text box.

Theoretically since the array is mirrored, it shouldn't matter the city order the output should be the same between say cleveland and canton compaired to canton and cleveland. If the cities are the same then the result is zero.

For reasons that I can't explain random numbers apper in the outbox but the above conditions do not happen. I've tried many diffrent ways of writing the code, but here is my latest version. If someone can look at it and point me into the right direction I would appreciate it.

VB.NET:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cities() As String = {"Canton", "Cincinnati", "Cleveland", "Columbus", "Dayton", "Toledo", "Youngstown"}
        Dim city1 As String = TextBox1.Text
        Dim city2 As String = TextBox2.Text

        Dim cityVal As Integer = 0
        Dim xVal As Integer = 0
        Dim yVal As Integer = 0
        Dim mi As Integer

        Dim miles(7, 7) As Integer
        Dim randomGenerator As New Random

        While cityVal <= 6
            For check1 As Integer = 0 To check1 <= 6
                cityVal = city1.CompareTo(cities(check1))
                If cityVal <= 6 Then
                    xVal = check1
                    check1 = 7
                    For check2 As Integer = 0 To check2 <= 6
                        cityVal = city2.CompareTo(cities(check2))
                        If cityVal <= 6 Then
                            yVal = check2
                            check2 = 7
                        Else
                            MsgBox("Invalid City!", MsgBoxStyle.Exclamation)
                        End If
                    Next check2
                Else
                    MsgBox("Invalid City!", MsgBoxStyle.Exclamation)
                End If
            Next check1
            cityVal = 7
        End While

        For x As Integer = 0 To 6
            For y As Integer = 0 To 6
                If x = y Then
                    miles(x, y) = 0
                End If
                mi = randomGenerator.Next(1, 1000)
                miles(x, y) = mi
                miles(y, x) = miles(x, y)
            Next
        Next

        Dim distance As Integer
        distance = miles(xVal, yVal)
        TextBox3.Text = distance

    End Sub

End Class
 
Last edited by a moderator:
Back
Top