rony2006

Calculate average, total and the biggest - from array

10 posts in this topic

Hello,

I have a listview and I converted the values from it in a 2D array and I need to calculate average, total and the biggest of the data from one column.

the 2d array is named $lt1 

$lt1[0][0] = is the number of rows (how many values I have) This number will change so one time $lt1[0][0]  is 10 for ex, another time $lt1[0][0]  is 4 or can be any number. The number of elements is not exact (n+1)

my values are in $lt1[1 to n+1][12]

So I need to calculate the average for the number $lt1[1][12], $lt1[2][12], $lt1[3][12], $lt1[n+1][12] .

 

How I can do this please?

Share this post


Link to post
Share on other sites



Maybe:

Local $average
For $n = 1 To $lt1[0][0]
    $average += $lt1[$n][12]
Next
$average = $average / $lt1[0][0]

4

Local $average
For $n = 1 To 4
    $average += $lt1[$n][12]
Next
$average = $average / 4

 


Regards,
 

Share this post


Link to post
Share on other sites

For the average is ok, but how I can calculate de maximum value that is in the array and the sum ?

Share this post


Link to post
Share on other sites

Within the same loop:

Local $average, $total, $max
For $n = 1 To $lt1[0][0]
    $average += $lt1[$n][12]
    $total += $lt1[$n][12]
    If $lt1[$n][12] > $max Then $max = $lt1[$n][12]
Next
$average = $average / $lt1[0][0]

 

1 person likes this

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Local $average, $BigVar = 0
For $n = 1 To $lt1[0][0]
    If $lt1[$n][12] > $BigVar Then $BigVar = $lt1[$n][12]
    $average += $lt1[$n][12]

Next
$average = $average / $lt1[0][0]
ConsoleWrite("! Average: " & $average & @CRLF)
ConsoleWrite("! Bigger Number: " & $BigVar & @CRLF)

 


Regards,
 

Share this post


Link to post
Share on other sites

Hello guys,

I tried both script but no one is working correct when calculating the maximum value from the array.

So I have this values:

0

0

0

0

3

0

6

0

0

144

 

Average value is: 15.3

Big value is: 6

Total 153

Why big value is 6 and not 144?

I used this code:

$lt1 = _GUIListViewEx_ReadToArray($listatickete, 1)
        _ArrayDisplay($lt1, "1D display")
        
Local $average, $total, $max
For $n = 1 To $lt1[0][0]
    $average += $lt1[$n][14]
    $total += $lt1[$n][14]
    If $lt1[$n][14] > $max Then $max = $lt1[$n][14]
Next
$average = $average / $lt1[0][0]


Msgbox (0, "average", $average)
Msgbox (0, "bigvar", $max)
    Msgbox (0, "total", $total)

 

Share this post


Link to post
Share on other sites

Probably because you've stored your values as strings and not integers or reals.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

and how i can convert them to integers? I take the data from a listview

 

Share this post


Link to post
Share on other sites

This detail was omitted from the OP. Try this:

Local $average, $total, $max
For $n = 1 To $lt1[0][0]
    $average += $lt1[$n][12]
    $total += $lt1[$n][12]
    If Number($lt1[$n][12]) > $max Then $max = Number($lt1[$n][12])
Next
$average = $average / $lt1[0][0]

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Working now, thanks!

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now