Home > OS >  The maximum of the minimum values in each column
The maximum of the minimum values in each column

Time:11-10

There is an array from n to n according to the given example
aij = 12 – 3i j
i - column number j- row number and need to find the Maximum of the minimum values in each column, I wrote the code, but it does not output what it should, please help

Sub task1()
 Dim i, j, m As Integer
    Dim a(5, 5) As Integer
    Dim max(5) As Integer
    
    For i = 0 To 4
        For j = 0 To 4
            a(i, j) = 12 - 3 * i - j
        Next j
    Next i

    For i = 0 To 4
        max(i) = a(i, 0)
        For j = 1 To 4
            If a(i, j) < max(i) Then max(i) = a(i, j)
        Next j
    Next i
        
    m = max(0)
    For i = 1 To 4
        If max(i) > m Then m = max(i)
    Next i
    
    MsgBox m

End Sub

CodePudding user response:

Get the Maximum of Column Minimums

  • The calculation of the mins was wrong as Ron Rosenfeld also shared in your comments.
  • I used this aij = 12 – 3i j which resulted in 2 * (6 - n).
  • If it is - j instead, as in your code, just change it in this code. Then the result will be 3 * (4 - n).

Keeping It Close

Sub Task1()
    
    Const n As Integer = 4
    If n < 0 Then Exit Sub
    
    Dim a(0 To n, 0 To n) As Integer
    Dim mins(0 To n) As Integer
    Dim i As Integer, j As Integer, max As Integer
    
    ' Populate the array.
    For i = 0 To n
        For j = 0 To n
            a(i, j) = 12 - 3 * i   j
        Next j
    Next i

    PrintData a, , " | ", "The Array" '***

    ' Get the column minimums.
    For j = 0 To n
        mins(j) = a(0, j)
        For i = 1 To n
            If a(i, j) < mins(j) Then mins(j) = a(i, j)
        Next i
        Debug.Print "The minimum of column " & j & " is " & mins(j) & "." '***
    Next j
        
    ' Get the maximum of the column minimums.
    max = mins(0)
    For i = 1 To n
        If mins(i) > max Then max = mins(i)
    Next i
    
    Debug.Print "The maximum of the column minimums is " & max & "." '***
    
    ' MsgBox max

End Sub

Debug.Print Result For n = 4

The Array
12 | 13 | 14 | 15 | 16
 9 | 10 | 11 | 12 | 13
 6 |  7 |  8 |  9 | 10
 3 |  4 |  5 |  6 |  7
 0 |  1 |  2 |  3 |  4
The minimum of column 0 is 0.
The minimum of column 1 is 1.
The minimum of column 2 is 2.
The minimum of column 3 is 3.
The minimum of column 4 is 4.
The maximum of the column minimums is 4.

Results

2 * (6 - n)

n   |  0  1  2  3  4  5  6  7  8  9
max | 12 10  8  6  4  2  0 -2 -4 -6

Getting Rid of the Mins Array

Sub GetMinsMax()
    
    Const n As Integer = 4
    If n < 0 Then Exit Sub
    
    Dim a(0 To n, 0 To n) As Integer
    Dim i As Integer, j As Integer, min As Integer, max As Integer
    
    For j = 0 To n
        For i = 0 To n
            ' Populate the current element.
            a(i, j) = 12 - 3 * i   j
            ' Update the column minimum.
            If i = 0 Then
                min = a(0, j)
            Else
                If a(i, j) < min Then min = a(i, j)
            End If
        Next i
        Debug.Print "The minimum of column " & j & " is " & min & "." '***
        ' Update the maximum of the column minimums.
        If j = 0 Then
            max = min
        Else
            If min > max Then max = min
        End If
    Next j
        
    PrintData a, , " | ", "The Array" '***
   
    Debug.Print "The maximum of the column minimums is " & max & "." '***
    
    ' MsgBox max

End Sub

Visualize

Sub PrintData( _
        ByVal Data As Variant, _
        Optional ByVal RowDelimiter As String = vbLf, _
        Optional ByVal ColumnDelimiter As String = " ", _
        Optional ByVal Title As String = "PrintData Result")
    
    ' Store the limits in variables
    Dim rLo As Long: rLo = LBound(Data, 1)
    Dim rHi As Long: rHi = UBound(Data, 1)
    Dim cLo As Long: cLo = LBound(Data, 2)
    Dim cHi As Long: cHi = UBound(Data, 2)
    
    ' Define the arrays.
    Dim cLens() As Long: ReDim cLens(rLo To rHi)
    Dim strData() As String: ReDim strData(rLo To rHi, cLo To cHi)
    
    ' For each column ('c'), store strings of the same length ('cLen')
    ' in the string array ('strData').
    
    Dim r As Long, c As Long
    Dim cLen As Long
    
    For c = cLo To cHi
        ' Calculate the current column's maximum length ('cLen').
        cLen = 0
        For r = rLo To rHi
            strData(r, c) = CStr(Data(r, c))
            cLens(r) = Len(strData(r, c))
            If cLens(r) > cLen Then cLen = cLens(r)
        Next r
        ' Store strings of the same length in the current column
        ' of the string array.
        If c = cHi Then ' last row (no column delimiter ('ColumnDelimiter'))
            For r = rLo To rHi
                strData(r, c) = Space(cLen - cLens(r)) & strData(r, c)
            Next r
        Else ' all but the last row
            For r = rLo To rHi
                strData(r, c) = Space(cLen - cLens(r)) & strData(r, c) _
                    & ColumnDelimiter
            Next r
        End If
    Next c
    
    ' Write the title to the print string ('PrintString').
    Dim PrintString As String: PrintString = Title
    
    ' Append the data from the string array to the print string.
    For r = rLo To rHi
        PrintString = PrintString & RowDelimiter
        For c = cLo To cHi
            PrintString = PrintString & strData(r, c)
        Next c
    Next r
    
    ' Print the print string.
    Debug.Print PrintString

End Sub
  • Related