none
Max and Min Numbers in Array RRS feed

  • Question

  • Hi All,

    I just to know how to find Maxima and minima in List R then show the digit count .

    Example at line 2 from text file the result show as bellow;

    Result line 2 is Max =12(2) at digits = 7,5

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.Data
    
    
    
    Public Class Form1
    
        Private R As New List(Of Integer)
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            TextBox1.Clear()
    
    
            'tox text file= "6005,1426,1385,5732,2229,3699,9950,8881,6223,0893,7574,3507,7538,4630,6523,2446,2223,5198,1997,4966,3500,0590,7527"
            '               "8992,4563,6716,8222,5077,7888,5657,1359,7325,1379,5359,9863,4108,4404,2832,5788,6083,2505,5301,9701,6266,7672,1792"
            '               "1344,7917,2893,4315,1381,6425,4200,3574,7456,1910,9244,6718,9413,1915,1590,5906,2135,2878,1951,0434,7359,1369,3184"
    
    
            Dim sr As New System.IO.StreamReader("C:\Users\family\Documents\Toc.txt")
    
            Do While sr.Peek <> -1
    
                Dim StringToCheck As String = sr.ReadLine()
    
                For i As Integer = 0 To 9
                    Dim StringToFind As String = CStr(i)
                    Dim Result As String = StringToCheck.Count(Function(c As Char) c = StringToFind).ToString
    
                    TextBox1.AppendText(CStr(i) & "  =>   " & Result & " " & vbCrLf)
                    R.Add(CInt(Result))
                Next
    
                Dim min As Integer = Integer.MaxValue, max As Integer = Integer.MinValue, Value As Integer = 0
    
                For i As Integer = 1 To R.Count - 1
                    Value = R(i)
    
                    If Value < min Then
                        min = Value
                    End If
    
                    If Value > max Then
                        max = Value
                    End If
                Next
    
                TextBox1.AppendText("Max Numbers =" & max & "     " & "Min Numbers =" & min & vbCrLf)
    
    
            Loop
    
        End Sub
    
    
    
    End Class
    

    Saturday, July 29, 2017 5:59 AM

Answers

  • Example at line 2 from text file the result show as bellow;

    You need to reset R each time the loop goes around.  Currently your min-max calculation is finding the min and max for all rows processed so far. You want the result for the current line only.

    Why are you excluding the first item from your calculation?

    • Marked as answer by mipakteh Saturday, July 29, 2017 7:07 AM
    Saturday, July 29, 2017 6:41 AM

All replies

  • Example at line 2 from text file the result show as bellow;

    You need to reset R each time the loop goes around.  Currently your min-max calculation is finding the min and max for all rows processed so far. You want the result for the current line only.

    Why are you excluding the first item from your calculation?

    • Marked as answer by mipakteh Saturday, July 29, 2017 7:07 AM
    Saturday, July 29, 2017 6:41 AM
  • yes,need to reset that Loop...then all lines calculation correct answer .Line 2 now correct max number = 12.

                Dim min As Integer = Integer.MaxValue, max As Integer = Integer.MinValue, Value As Integer = 0
    
                For i As Integer = 1 To R.Count - 1
    
                    Value = R(i)
    
                    If Value < min Then
                        min = Value
                    End If
    
                    If Value > max Then
                        max = Value
                    End If
                Next
    
                TextBox1.AppendText("Max Numbers =" & max & "     " & "Min Numbers =" & min & vbCrLf)
    
                R.Clear()
    
            Loop

    Acamar,

    How to show that max number at line 1 is 12 and the digit is 2'

    At line 2 show max number is 12 and the digit is 5,7

    At line 3 show max number is 17 and the digit is 1.

    Saturday, July 29, 2017 7:06 AM
  • How to show that max number at line 1 is 12 and the digit is 2'

    Please don't keep asking additional questions.  If you cannot state the complete task in the first post then you need to start a new thread.

    You can't find which values created that maximum or minimum because you are not keeping a record of your results.  Just writing the results to a text box is not an adequate record. 

    If you create a custom class that defines the objects you are creating then you can use a collection (eg, a List(Of ) those class instances to do any processing you need, such as finding the min and max, finding which items in that collection have that min or max, and printing out the information about that item,.  It appears that your custom classes need to have the integer value (0 to 9), the count for that value, and a string representation of the object (eg, "0 => 10") for showing in the text box. If you have a collection of the4se objects then when you have found the minimum and maximum you can loop over that collection and show each one where the value matches that minimum or maximum.
    See: https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/objects-and-classes/

    Saturday, July 29, 2017 7:38 AM