Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Please help me with a 2D array

Lindokuhle MlindoLindokuhle Mlindo Member Posts: 1

Consider Table 2 below, with 5 rows and 7 columns, in which, each cell is either valid(V0 or invalid(I) Assume the values in a table 2are stored in a multi-dimension array write a function procedure, using the nested for….next statement to calculate the percentage of the valid cells in a Table

The formula to percentage of a valid cells is
(number of valid cells/ total number of cells) * 100

0   1   2   3   4   5   6

0 V I V I V V I
1 I I V I V V V
2 I V I V I I V
3 V V I V I I V
4 1 V I I V V I

Answers

  • Tyson BlairTyson Blair Member Posts: 35

    Hello, Here is a simple example
    ` Dim Table = New Char(4, 6) {{"V", "I", "V", "I", "V", "V", "I"}, {"I", "I", "V", "I", "V", "V", "V"}, {"I", "V", "I", "V", "I", "I", "V"}, {"V", "V", "I", "V", "I", "I", "V"}, {"I", "V", "I", "I", "V", "V", "I"}}
    Sub CalculateValidCells(ByVal Table(,) As Char, ByVal RowSize As Integer, ByVal CellSize As Integer)
    ' Dim RowLen As Integer = GetRowLen(Table, 0)
    'Dim CellLen As Integer = GetCellLen(Table, 0)
    Dim RowLen As Integer = RowSize
    Dim CellLen As Integer = CellSize

        Dim TotalCells As Integer = 0
        Dim ValidCells As Integer = 0
    
        For Rows = 0 To RowLen - 1
            For Cells = 0 To CellLen - 1
                'Console.WriteLine("Rows {0}", Rows)
                'Console.WriteLine("Cells {0}", Cells)
                If Table(Rows, Cells) = "V" Then
                    TotalCells = TotalCells + 1
                    ValidCells = ValidCells + 1
    

    `

  • Tyson BlairTyson Blair Member Posts: 35

    `
    Dim Table = New Char(4, 6) {{"V", "I", "V", "I", "V", "V", "I"}, {"I", "I", "V", "I", "V", "V", "V"}, {"I", "V", "I", "V", "I", "I", "V"}, {"V", "V", "I", "V", "I", "I", "V"}, {"I", "V", "I", "I", "V", "V", "I"}}
    Sub CalculateValidCells(ByVal Table(,) As Char, ByVal RowSize As Integer, ByVal CellSize As Integer)
    ' Dim RowLen As Integer = GetRowLen(Table, 0)
    'Dim CellLen As Integer = GetCellLen(Table, 0)
    Dim RowLen As Integer = RowSize
    Dim CellLen As Integer = CellSize

        Dim TotalCells As Integer = 0
        Dim ValidCells As Integer = 0
    
        For Rows = 0 To RowLen - 1
            For Cells = 0 To CellLen - 1
                'Console.WriteLine("Rows {0}", Rows)
                'Console.WriteLine("Cells {0}", Cells)
                If Table(Rows, Cells) = "V" Then
                    TotalCells = TotalCells + 1
                    ValidCells = ValidCells + 1
    

    `

  • Tyson BlairTyson Blair Member Posts: 35
    Dim Table = New Char(4, 6) {{"V", "I", "V", "I", "V", "V", "I"}, {"I", "I", "V", "I", "V", "V", "V"}, {"I", "V", "I", "V", "I", "I", "V"}, {"V", "V", "I", "V", "I", "I", "V"}, {"I", "V", "I", "I", "V", "V", "I"}}
    Sub CalculateValidCells(ByVal Table(,) As Char, ByVal RowSize As Integer, ByVal CellSize As Integer)
        ' Dim RowLen As Integer = GetRowLen(Table, 0)
        'Dim CellLen As Integer = GetCellLen(Table, 0)
        Dim RowLen As Integer = RowSize
        Dim CellLen As Integer = CellSize
    
        Dim TotalCells As Integer = 0
        Dim ValidCells As Integer = 0
    
        For Rows = 0 To RowLen - 1
            For Cells = 0 To CellLen - 1
                'Console.WriteLine("Rows {0}", Rows)
                'Console.WriteLine("Cells {0}", Cells)
                If Table(Rows, Cells) = "V" Then
                    TotalCells = TotalCells + 1
                    ValidCells = ValidCells + 1
                ElseIf Table(Rows, Cells) = "I" Then
                    TotalCells = TotalCells + 1
                Else
                    TotalCells = TotalCells + 1
                End If
            Next
        Next
        Console.WriteLine("Total Rows {0}", RowLen - 1)
        Console.WriteLine("Total Cells {0}", TotalCells)
        Console.WriteLine("Total Valid Cells {0}", ValidCells)
        Console.WriteLine("% of Valid Cells {0}", (ValidCells / TotalCells) * 100)
        'Console.WriteLine("Total Cells {0}", TotalCells)
        Console.ReadLine()
    End Sub
    Sub Main()
        CalculateValidCells(Table, 5, 7)
    End Sub
    
Sign In or Register to comment.