Dsmoeg999 Posted April 27 Share Posted April 27 Hello everyone in this example I have an array of 10 elements I'm going through it and I'm finding the largest number I have put a counter to calculate the number of times it repeats. Can you guide me to apply this correctly Best regards expandcollapse popup#include <MsgBoxConstants.au3> Local $array[10] $array[0]=22 $array[1]=13 $array[2]=11 $array[3]=99 $array[4]=2 $array[5]=17 $array[6]=99 $array[7]=23 $array[8]=99 $array[9]=74 $max=$array[0] $counter=0 For $i = 0 To 9 Step 1 MsgBox($MB_SYSTEMMODAL, "", "The number is!" & @CRLF & $array[$i]) If $array[$i] > $max Then $max=$array[$i] $counter+=1 EndIf Next MsgBox($MB_SYSTEMMODAL, "", "The max value is!" & @CRLF & $max) MsgBox($MB_SYSTEMMODAL, "", "And its repited:" & @CRLF & $counter) Link to comment Share on other sites More sharing options...
RTFC Posted April 27 Share Posted April 27 #include <Array.au3> Local $array[10] $array[0]=22 $array[1]=13 $array[2]=11 $array[3]=99 $array[4]=2 $array[5]=17 $array[6]=99 $array[7]=23 $array[8]=99 $array[9]=74 $maxValue=_ArrayMax($array) $list=_ArrayFindAll($array,$maxValue) ConsoleWrite("Max value " & $maxValue & " occurs " & $list[0] & " time(s)." & @CRLF) Dsmoeg999 1 My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
Dsmoeg999 Posted April 27 Author Share Posted April 27 Thank you and following the example I propose, I would also like to know how to do it with a counter. Link to comment Share on other sites More sharing options...
Solution Nine Posted April 27 Solution Share Posted April 27 (edited) 13 minutes ago, Dsmoeg999 said: I would also like to know how to do it with a counter. Local $array[10] $array[0]=22 $array[1]=13 $array[2]=11 $array[3]=99 $array[4]=2 $array[5]=17 $array[6]=99 $array[7]=23 $array[8]=99 $array[9]=74 Local $max = $array[0], $counter = 1 For $i = 1 To UBound($array) - 1 If $array[$i] > $max Then $max=$array[$i] $counter = 1 ElseIf $array[$i] = $max Then $counter += 1 EndIf Next MsgBox($MB_SYSTEMMODAL, "", "The max value is!" & @CRLF & $max) MsgBox($MB_SYSTEMMODAL, "", "And its repited:" & @CRLF & $counter) Edited April 27 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Link to comment Share on other sites More sharing options...
Alecsis1 Posted April 27 Share Posted April 27 One more example #include <Array.au3> ; Local $array[10], $i, $counter, $maxvalue ; $array[0]=22 $array[1]=13 $array[2]=11 $array[3]=99 $array[4]=2 $array[5]=17 $array[6]=99 $array[7]=23 $array[8]=99 $array[9]=74 _ArraySort($array, 1) ; sort in descending order ; $maxvalue = $array[0] ; max value $counter = 1 ; # of max value detections ; For $i = 1 To UBound($array)-1 ; after sort $array[0] is the <max> by definition If $array[$i] = $maxvalue Then $counter +=1 Next ConsoleWrite("Max value " & $maxValue & " occurs " & $counter & " time(s)." & @CRLF) ; --- end of job --- pixelsearch 1 Link to comment Share on other sites More sharing options...
mistersquirrle Posted April 27 Share Posted April 27 @Nine's is the solution that I would do as well, as it's the simplest and I would assume fastest. I was curious though how the suggestions here stacked up, so I did some tests. _Occurance_Max_Find = @RTFC _Occurance_Counter = @Nine _Occurance_Sort_Count = @Alecsis1 I did make a change to Alexsis1's solution to exit the for loop when $array[$i] <> $maxvalue, since it's sorted, exits a bit sooner When using a 10 row array: [====================== Run progress, 10,000 times * 3 functions ======================] +———————————————————————+—————————————————+———————————————+——————————————+—————————————+ | Function Name | Time Taken (ms) | Avg Time (ms) | Std Dev (ms) | Faster By % | +———————————————————————+—————————————————+———————————————+——————————————+—————————————+ | _Occurance_Max_Find | 4074.57 | 0.4075 | 0.0849 | 0.00 | +| _Occurance_Counter | 185.19 | 0.0185 | 0.0086 | 95.46 | | _Occurance_Sort_Count | 893.57 | 0.0894 | 0.0313 | 78.07 | +———————————————————————+—————————————————+———————————————+——————————————+—————————————+ When increasing the size to 100: [====================== Run progress, 10,000 times * 3 functions ======================] +———————————————————————+—————————————————+———————————————+——————————————+—————————————+ | Function Name | Time Taken (ms) | Avg Time (ms) | Std Dev (ms) | Faster By % | +———————————————————————+—————————————————+———————————————+——————————————+—————————————+ | _Occurance_Max_Find | 8702.17 | 0.8702 | 0.1885 | 30.33 | +| _Occurance_Counter | 1213.34 | 0.1213 | 0.0436 | 90.29 | | _Occurance_Sort_Count | 12490.12 | 1.2490 | 0.2485 | 0.00 | +———————————————————————+—————————————————+———————————————+——————————————+—————————————+ And then 10,000: [======================= Run progress, 100 times * 3 functions ========================] +———————————————————————+—————————————————+———————————————+——————————————+—————————————+ | Function Name | Time Taken (ms) | Avg Time (ms) | Std Dev (ms) | Faster By % | +———————————————————————+—————————————————+———————————————+——————————————+—————————————+ | _Occurance_Max_Find | 5833.96 | 58.3396 | 4.6752 | 72.94 | +| _Occurance_Counter | 1181.70 | 11.8170 | 1.2320 | 94.52 | | _Occurance_Sort_Count | 21559.50 | 215.5950 | 9.1349 | 0.00 | +———————————————————————+—————————————————+———————————————+——————————————+—————————————+ It's interesting that Sorting is a lot faster when the array size is smaller, but it quickly loses speed. It switches somewhere around ~50-60 indexes, though the data also likely affects it quite a bit. Dsmoeg999 1 We ought not to misbehave, but we should look as though we could. Link to comment Share on other sites More sharing options...
Alecsis1 Posted April 27 Share Posted April 27 To avoid excessive useless comparisons replace For $i = 1 To UBound($array)-1 ; after sort $array[0] is the <max> by definition If $array[$i] = $maxvalue Then $counter +=1 Next with For $i = 1 To UBound($array)-1 ; after sort $array[0] is the <max> by definition If $array[$i] < $maxvalue Then ExitLoop ; eliminate redundant compares $counter +=1 Next Test with array of 1 million numbers expandcollapse popup#include <Array.au3> ; Local $array[1000000], $i, $counter, $maxvalue ; $array[0]=22 $array[1]=13 $array[2]=11 $array[3]=99 $array[4]=2 $array[5]=17 $array[6]=99 $array[7]=23 $array[8]=99 $array[9]=74 Local $hTimer, $DeltaT ConsoleWrite("Array size = " & UBound($array) & @CRLF) $hTimer = TimerInit() For $i = 10 To UBound($array)-1 $array[$i] = Random(0, 98, 1) Next $DeltaT = TimerDiff($hTimer) ConsoleWrite("Delta T (init) = " & $DeltaT & @CRLF) $hTimer = TimerInit() _ArraySort($array, 1) ; sort in descending order $DeltaT = TimerDiff($hTimer) ConsoleWrite("Delta T (sort) = " & $DeltaT & @CRLF) ; $maxvalue = $array[0] ; max value $counter = 1 ; # of max value detections ; $hTimer = TimerInit() For $i = 1 To UBound($array)-1 ; after sort $array[0] is the <max> by definition If $array[$i] < $maxvalue Then ExitLoop ; eliminate redundant compares $counter +=1 Next $DeltaT = TimerDiff($hTimer) ConsoleWrite("Delta T (count) = " & $DeltaT & @CRLF) ConsoleWrite("Max value " & $maxValue & " occurs " & $counter & " time(s)." & @CRLF) ; --- end of job --- >"C:\DevTools\AutoIt3\SciTE\..\AutoIt3.exe" /ErrorStdOut "D:\Alecsis\Prog\AutoIt\_Debug\tt4.au3" Array size = 1000000 Delta T (init) = 1436.309 Delta T (sort) = 52488.0108 Delta T (count) = 0.0094 Max value 99 occurs 3 time(s). >Exit code: 0 Time: 54.13 Dsmoeg999 1 Link to comment Share on other sites More sharing options...
Dsmoeg999 Posted April 28 Author Share Posted April 28 (edited) Hello friends thanks for the examples I appreciate it. I would like to clarify a doubt at the moment the exchange occurs If $array[$i] > $max Then $max=$array[$i] ;Why we cant increase here the counter $counter+=1 EndIf ;This also does not give us the correct result. if $array[$i] = $max Then $counter+=1 Endif ;This looks like the right way to check it ElseIf $array[$i] = $max Then $counter += 1 EndIf I understand that it is so because the exchange occurs more times but really what you have to compare is if the greater is equal to the greater then it is repeated. Can you confirm if I understand correctly ? Edited April 28 by Dsmoeg999 Link to comment Share on other sites More sharing options...
mistersquirrle Posted April 28 Share Posted April 28 I don't really understand your question, seems like a bit of a language barrier. Are you able to post the full code that you're having a problem with? Just what's posted above isn't really enough to go on, not sure how you're using these. However, this is incorrect: 12 hours ago, Dsmoeg999 said: If $array[$i] > $max Then $max=$array[$i] ;Why we cant increase here the counter $counter+=1 EndIf You can't increase the counter here because you've found a new max, the occurrence count needs to be reset to 1, so it should be just $counter = 1, not +=. You need to reset the count to 1 each time you find a new max, because you asked for how to find the occurrences of the max value. We ought not to misbehave, but we should look as though we could. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now