?
Solved

Adding values in an array

Posted on 2012-03-19
7
Medium Priority
?
214 Views
Last Modified: 2012-03-20
I am relatively new at working with arrays.  I want to add the values in one array to the values in a second array.  Something like this

Dim array1() as double
Dim array2() as Double
Dim array3() as double

array1(0) = 2
array1(1) = 5

array2(0) = 1
array2(1) = 6

this is where the line I don't know how to write would go.

If I were to debug.print array3() I want to see the following

3
11

Thanks very much in advance.
Ron
0
Comment
Question by:rfreud
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 37739516
Something like:
Dim i As Long
For i = LBound(array1) To UBound(array1)
    array3(i) = array1(i) + array2(i)
    Debug.Print "array3(" & i & ") = " & array3(i)
Next i

Open in new window

0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37739535
This works fine for me as well also:
Dim array1(0 To 100) As Double
Dim array2(0 To 100) As Double
Dim array3(0 To 100) As Double

array1(0) = 2
array1(1) = 5

array2(0) = 1
array2(1) = 6

array3(0) = array1(0) + array2(0)
array3(1) = array1(1) + array2(1)

Debug.Print array3(0) & vbCrLf & array3(1)
0
 

Author Comment

by:rfreud
ID: 37739549
I will give this a try tomorrow...  
Thanks,
Ron
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 30

Expert Comment

by:hnasr
ID: 37740015
If upper bound for first array differs from the second:

Private Sub Command11_Click()
    Dim array1() As Double
    Dim array2() As Double
    Dim array3() As Double
   
    ReDim array1(3)
    ReDim array2(5)
    ReDim array3(5)
   
    Dim i As Integer
    For i = 0 To UBound(array1)
        array1(i) = i
        Debug.Print i
    Next
    For i = 0 To UBound(array2)
        array2(i) = i
        Debug.Print i
    Next
    If UBound(array1) >= UBound(array2) Then
        For i = 0 To UBound(array1)
            array3(i) = array1(i)
        Next
        For i = 0 To UBound(array2)
            array3(i) = array1(i) + array2(i)
        Next
    Else
        For i = 0 To UBound(array2)
            array3(i) = array2(i)
        Next

        For i = 0 To UBound(array1)
            array3(i) = array1(i) + array2(i)
        Next
    End If
    For i = 0 To UBound(array3)
        Debug.Print array3(i)
    Next
End Sub
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37740024
Yeah...I put absolutely no error checking in my snippet; just assumed that all arrays were the same size and had the same lower/upper/bounds.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 37740317
And now you run into the age old problem of storing derivable data.  There you are with a1, a2, and a3.  Someone changes a value in a1 or a2, but does not update a3 which is the sum - now what?  You have to make sure every time you view the contents of a3, it is in fact re-created.
0
 

Author Closing Comment

by:rfreud
ID: 37741991
Thank you very very much, particularly for the additional comments.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question