Sum of an Array! Not covered in FAQs...

# Thread: Sum of an Array! Not covered in FAQs...

1. Senior Member
Join Date
Dec 1969
Posts
450

## Sum of an Array! Not covered in FAQs...

Dim otherNames(3)<BR><BR> Names(0) = "1"<BR> Names(1) = "2"<BR> Names(2) = "3"<BR><BR> Summing its values<BR> Response.Write sum(names)<BR>

2. Senior Member
Join Date
Dec 1969
Posts
450

## RE: Also sort an array ascending Thanks!

nt

3. Senior Member
Join Date
Dec 1969
Posts
196

## RE: Sum of an Array! Not covered in FAQs...

Function arraySum(arr)<BR> iSum = 0<BR> For iCount = 0 to uBound(arr)<BR> iSum = iSum + arr(iCount)<BR> Next<BR> arraySum = iSum<BR>End Function<BR><BR><BR><BR>- jørn

4. Senior Member
Join Date
Dec 1969
Posts
196

## RE: Also sort an array ascending Thanks!

This is in the faqs... Search for quicksort or something....<BR><BR><BR><BR><BR>- jørn

5. Senior Member
Join Date
Dec 1969
Posts
5,104

## To be safe...

Do a CDbl when you add to the sum.<BR><BR>Function arraySum(arr)<BR> iSum = 0<BR> For iCount = 0 to uBound(arr)<BR> iSum = iSum + CDbl(arr(iCount))<BR> Next<BR><BR> arraySum = iSum<BR>End Function<BR><BR>-Doug

6. Senior Member
Join Date
Dec 1969
Posts
196

## To be safer... ;)

I&#039;d rather use isNumeric to be safe....<BR><BR><BR>Function arraySum(arr)<BR>iSum = 0<BR>For iCount = 0 to uBound(arr)<BR>If isNumeric(arr(iCount)) Then iSum = iSum + arr(iCount)<BR>Next<BR><BR>arraySum = iSum<BR>End Function<BR><BR><BR><BR>- jørn<BR>

7. Senior Member
Join Date
Dec 1969
Posts
450

## You guys are on the ball 2day!!! Thanks! 1 last qu

I have a recordset which gives a listing of names. I also have a variable combining all accounts for each contestant that lists total dollar amt.<BR><BR>What would be the proper syntax to put the contestant AND the total \$\$\$ value for that specific contestant in an array or recordset and sort by ascending \$\$\$ value.<BR><BR>Example:<BR> <BR>Contestant Total Value:<BR>----------- ------------<BR><BR>Bryan 500,000<BR>Dsetzer 1,000,000<BR>Jorn 999,999<BR><BR><BR>It will list ascending<BR><BR>Dsetzer 1,000,000<BR>Jorn 999,000<BR>Bryan 500,000<BR>

8. Senior Member
Join Date
Dec 1969
Posts
5,104

## If we are going to get crazy about it...

Let&#039;s do a couple of things.<BR><BR>1) Make the function name more descriptive.<BR>2) Put On Error Resume Next w/i the function<BR>3) Declare local variables to hold Sum & Counter<BR>4) Check to see if the arr parameter is an array<BR>5) Check to see if each arr value is a numeric value. If it is, convert it to a double while adding (just to make sure artithemtic is happening and not concatenation).<BR><BR><BR>Function getArraySum(arr)<BR>On Error Resume Next<BR><BR>Dim m_iCnt<BR>Dim m_iSum<BR> m_iSum = 0<BR><BR> If IsArray(arr) Then<BR> For m_iCnt = 0 to uBound(arr)<BR> If isNumeric(arr(m_iCnt)) Then<BR> m_iSum = m_iSum+ CDbl(arr(m_iCnt))<BR><BR> End If<BR><BR> Next<BR><BR> End If<BR><BR> getArraySum = m_iSum<BR><BR>End Function<BR><BR>-Doug

9. Senior Member
Join Date
Dec 1969
Posts
450

## Looks good.... Thank you, kind sir.

nt

10. Senior Member
Join Date
Dec 1969
Posts
450

## Sorting a Two-Dimensional Array with Bubble Sort F

nt

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•