S
sal
Greets, All
Converting array formula to work with datatables/dataset tia sal
I finally completed a formula I was working on, see working code below.
I would like to change this code so it will work with a variable mutl-
row, 5 column datatable where the users select items. Anyone have any
suggestions on where to start?
Or changes in the current code that might be made.
Private Sub Arraylooptest_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Arraylooptest.Click
Dim arraya() As Integer = {15, 17, 20, 25} 'variable numbers
users can change
Dim arrayb() As Integer
Dim arrayc() As Integer
Dim i, k As Integer, total As String
Dim v As Object
ReDim arrayb(arraya.GetUpperBound(0) - 1)
arrayb(0) = arraya(0) + arraya(1)
Console.WriteLine("Numbers used ")
For Each v In arraya
Console.WriteLine((v))
Next
Console.WriteLine(String.Format("a(0)->({0}) + a(1)->({1})= b
(0)->({2})", arraya(0), arraya(1), arrayb(0)))
For i = 1 To arraya.GetUpperBound(0) - 1
Console.WriteLine(String.Format("b({0})->({1}) + a({2})->
({3})= b({4})->({5})", i - 1, arrayb(i - 1), (i + 1), arraya(i + 1), i,
arrayb(i - 1) + arraya(i + 1)))
arrayb(i) = arrayb(i - 1) + arraya(i + 1)
Next
ReDim arrayc(arrayb.GetUpperBound(0) - 1)
'If statement to break if only two elements selected
If (arrayc.Length) = 0 Then
MessageBox.Show("only two elements use your calculator ")
Exit Sub
Else
End If
arrayc(0) = arrayb(0) + arrayb(1)
Console.WriteLine("==Loop of Sums==")
Console.WriteLine(String.Format("b({0})->({1}) + b({2})->({3})= c
({4})->({5})", 0, arrayb(0), 1, arrayb(1), 0, arrayb(0) + arrayb(1)))
For k = 1 To arrayb.GetUpperBound(0) - 1
arrayc(k) = arrayc(k - 1) + arrayb(k + 1)
Console.WriteLine(String.Format("c({0})->({1}) + b({2})->
({3})= c({4})->({5})", k - 1, arrayc(k - 1), k + 1, arrayb(k + 1), k,
arrayc(k)))
Next
ReDim Preserve arrayc(k)
'Show overall answer or puts answer in next open array slot
If k = 1 Then
Exit Sub
Else
arrayc(k) = arrayc(k - 2) + arrayc(k - 1)
Console.WriteLine(String.Format("c({0})->({1}) + c({2})->
({3})= c({4})->({5})", k - 2, arrayc(k - 2), k - 1, arrayc(k - 1), k,
arrayc(k)))
End If
End Sub
This is the output
Numbers used
15
17
20
25
a(0)->(15) + a(1)->(17)= b(0)->(32)
b(0)->(32) + a(2)->(20)= b(1)->(52)
b(1)->(52) + a(3)->(25)= b(2)->(77)
==Loop of Sums==
b(0)->(32) + b(1)->(52)= c(0)->(84)
c(0)->(84) + b(2)->(77)= c(1)->(161)
c(0)->(84) + c(1)->(161)= c(2)->(245)
PS. Thanks to everyone who got me this far.
I sure have learned alot.
TIA
Converting array formula to work with datatables/dataset tia sal
I finally completed a formula I was working on, see working code below.
I would like to change this code so it will work with a variable mutl-
row, 5 column datatable where the users select items. Anyone have any
suggestions on where to start?
Or changes in the current code that might be made.
Private Sub Arraylooptest_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Arraylooptest.Click
Dim arraya() As Integer = {15, 17, 20, 25} 'variable numbers
users can change
Dim arrayb() As Integer
Dim arrayc() As Integer
Dim i, k As Integer, total As String
Dim v As Object
ReDim arrayb(arraya.GetUpperBound(0) - 1)
arrayb(0) = arraya(0) + arraya(1)
Console.WriteLine("Numbers used ")
For Each v In arraya
Console.WriteLine((v))
Next
Console.WriteLine(String.Format("a(0)->({0}) + a(1)->({1})= b
(0)->({2})", arraya(0), arraya(1), arrayb(0)))
For i = 1 To arraya.GetUpperBound(0) - 1
Console.WriteLine(String.Format("b({0})->({1}) + a({2})->
({3})= b({4})->({5})", i - 1, arrayb(i - 1), (i + 1), arraya(i + 1), i,
arrayb(i - 1) + arraya(i + 1)))
arrayb(i) = arrayb(i - 1) + arraya(i + 1)
Next
ReDim arrayc(arrayb.GetUpperBound(0) - 1)
'If statement to break if only two elements selected
If (arrayc.Length) = 0 Then
MessageBox.Show("only two elements use your calculator ")
Exit Sub
Else
End If
arrayc(0) = arrayb(0) + arrayb(1)
Console.WriteLine("==Loop of Sums==")
Console.WriteLine(String.Format("b({0})->({1}) + b({2})->({3})= c
({4})->({5})", 0, arrayb(0), 1, arrayb(1), 0, arrayb(0) + arrayb(1)))
For k = 1 To arrayb.GetUpperBound(0) - 1
arrayc(k) = arrayc(k - 1) + arrayb(k + 1)
Console.WriteLine(String.Format("c({0})->({1}) + b({2})->
({3})= c({4})->({5})", k - 1, arrayc(k - 1), k + 1, arrayb(k + 1), k,
arrayc(k)))
Next
ReDim Preserve arrayc(k)
'Show overall answer or puts answer in next open array slot
If k = 1 Then
Exit Sub
Else
arrayc(k) = arrayc(k - 2) + arrayc(k - 1)
Console.WriteLine(String.Format("c({0})->({1}) + c({2})->
({3})= c({4})->({5})", k - 2, arrayc(k - 2), k - 1, arrayc(k - 1), k,
arrayc(k)))
End If
End Sub
This is the output
Numbers used
15
17
20
25
a(0)->(15) + a(1)->(17)= b(0)->(32)
b(0)->(32) + a(2)->(20)= b(1)->(52)
b(1)->(52) + a(3)->(25)= b(2)->(77)
==Loop of Sums==
b(0)->(32) + b(1)->(52)= c(0)->(84)
c(0)->(84) + b(2)->(77)= c(1)->(161)
c(0)->(84) + c(1)->(161)= c(2)->(245)
PS. Thanks to everyone who got me this far.
I sure have learned alot.
TIA