Link to home
Start Free TrialLog in
Avatar of RyanBank
RyanBank

asked on

add 3 elements

Hi,

Please kindly assist how can we do the following:

arr_req=1,2,3,4,5,6,7,8,9,10
add 3 elements from the current element and move to the next element and loop.

1+2+3=6 -element 1
2+3+4=9 -element 2
...
for the element that will be less than the slots, continue with the first element.
9+10+1=element 9
10+1+2=element 10

output:
arr_reqconverted=6,9,12,15,18,21,24,27,20,13



dim intSumofSlots as integer
arr_req=1,2,3,4,5,6,7,8,9,10
Dim Arr_ReqConverted() As String


For i = 0 To UBound(arr_req)
intSumOfSlots = 0
For x = 0 To intSlot - 1

If i + x < 11 Then
intSumOfSlots = intSumOfSlots + i+x
else
intSumOfSlots = intSumOfSlots + CInt(arr_req(i + x - 10))
endif
next
Arr_ReqConverted(i) = intSumOfSlots
next
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image


dim intSumofSlots as integer
Dim Arr_ReqConverted() As String

For i = 0 To UBound(arr_req)
  arr_req(i) = i
next

for i = UBound(arr_req)
  intSumOfSlots = 0
  for x = i to i + 2
    if x < ubound(arr_req) then
       intSumOfSlots = intSumOfSlots + arr_req(x)
  next x
  arr_req(i)= intSumOfSlots
next i

Avatar of RyanBank
RyanBank

ASKER

not sure What I have type in wrongly but

Output came out:3,6,9,12,15,18,21,15,8,0

output should be:
arr_reqconverted=6,9,12,15,18,21,24,27,20,13




Private Sub Command1_Click()
Dim intSumofslots As Integer
ReDim Preserve arr_req(9)
For i = 0 To UBound(arr_req)
  arr_req(i) = i
Next
For i = 0 To UBound(arr_req)
  intSumofslots = 0
  For x = i To i + 2
    If x < UBound(arr_req) Then intSumofslots = intSumofslots + arr_req(x)
  Next x
  arr_reqConverted(i) = intSumofslots
Next i
Debug.Print Join(arr_reqConverted, ",")

End Sub


Private Sub Command1_Click()
Dim intSumofslots As Integer
ReDim Preserve arr_req(9)
For i = 0 To UBound(arr_req)
  arr_req(i) = i+1
Next
For i = 0 To UBound(arr_req)
  intSumofslots = 0
  For x = i To i + 2
    If x < UBound(arr_req) Then intSumofslots = intSumofslots + arr_req(x)
  Next x
  arr_reqConverted(i) = intSumofslots
Next i
Debug.Print Join(arr_reqConverted, ",")

End Sub

Private Sub Command1_Click()
Dim intSumofslots As Integer
ReDim Preserve arr_req(9)
For i = 0 To UBound(arr_req)
  arr_req(i) = i + 1  <<<< correction here
Next
For i = 0 To UBound(arr_req)
  intSumofslots = 0
  For x = i To i + 2
    If x <= UBound(arr_req) Then intSumofslots = intSumofslots + arr_req(x)     <<<< correction here
  Next x
  arr_reqConverted(i) = intSumofslots
Next i
Debug.Print Join(arr_reqConverted, ",")

End Sub
Finish changing, sad to say not a complete match:

Output came out:6,9,12,15,18,21,24,17,9,0


output should be:
arr_reqconverted=6,9,12,15,18,21,24,27,20,13
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
chagne this

For x = i To i + 2
    If x <= UBound(arr_req) Then intSumofslots = intSumofslots + arr_req(x)     <<<< correction here
  Next x


to

For x = i To i + 2
    If x <= UBound(arr_req) Then
        intSumofSlots = intSumofSlots + arr_req(x)     '<<<< correction here
    Else
        intSumofSlots = intSumofSlots + arr_req(x - 10)
    End If
  Next x
don't accept my comment as answer since angleIII almost got it
I added a "-1" in the code below, please kindly let me know if this is correct?



Dim intSumofslots As Integer
ReDim Preserve arr_req(9)
ReDim Preserve Arr_ReqConverted(9)

For i = 0 To UBound(arr_req)
  arr_req(i) = i + 1
Next
For i = 0 To UBound(arr_req)
  intSumofslots = 0
  For x = i To i + 3
    If x <= UBound(arr_req) Then
      intSumofslots = intSumofslots + arr_req(x)
    Else
      intSumofslots = intSumofslots + arr_req(x - UBound(arr_req) + LBound(arr_req) - 1)   <-----here
    End If
  Next x
  Arr_ReqConverted(i) = intSumofslots
Next i
Debug.Print Join(Arr_ReqConverted, ",")
yes, that's correct. I forgot that (it's difficult to test without vb installed)