Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ARRAY COMPUTATIONS

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How will i write this in vb code, I've got a formula here :
A = d/3 [h1 + hn + 2(h3+h5+ ... hn-2) + 4[h2 + h4 + ... hn-1] where in n = the maximum number for the sequence
 
Dim nResult as Integer
Dim nTemp1 As Integer
Dim nTemp2 As Integer

nTemp1 = 0
nTemp2 = 0
For i = 3 to n - 2 step 2
nTemp1 = nTemp1 + (h * n)
Next i
For i = 2 to n - 1 step 2
nTemp2 = nTemp2 + (h * n)
Next i
nResult = (d / 3) * ((h * 1) + (h * n) + (2 * nTemp1) + (4 * nTemp2))

So, actually, you won't even need to use an array :) *wink* Best Regards and many Thanks!
Michael G. Bronner X-)

"He was a wise man who invented beer." Plato
 
INPUT
hn - where n is the maximum number
h1 - value for h1

for example the value for hn = 100 so i must input the value for h1 ... h100

thanx for your answer but the problem is i need to input the values for h1, h2, and so on until it reaches the hn, hope you could give me the answer.

 
O.K. Huck this is Tom (Sawyer?). I think MichaelBronner (and I) understood the issue incorrectly. You ARE saying that you WANT (or NEED) to enter 100 (or so) VALUES by HAND? You MEAN MANUAL effort of KeyBoard? And the number of values needs to be "flexable"?

Please confirm. This Can be done, but it is not just another white wash job (or is it?)

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Huck, I had the answer posted last night, but then the servers crashed... just my luck. her'tis:

Private Function GetNum(nArray As Integer, n As Integer) As Float
//n is the total number of elements in nArray
Dim nResult as Integer
Dim nTemp1 As Integer
Dim nTemp2 As Integer

nTemp1 = 0
nTemp2 = 0
For i = 2 to n - 3 step 2
nTemp1 = nTemp1 + (h * n)
Next i
For i = 1 to n - 2 step 2
nTemp2 = nTemp2 + (h * n)
Next i
GetNum = (d / 3) * (nArray[0] + nArray[n] + 2 * nTemp1 + 4 * nTemp2)
End Function



That should do it :) Best Regards and many Thanks!
Michael G. Bronner X-)

"He was a wise man who invented beer." Plato
 
an easier way to get the last element (better than passing it would be:

Private Function GetNum(nArray As Integer) As Float
Dim n As Integer
Dim nResult as Integer
Dim nTemp1 As Integer
Dim nTemp2 As Integer

n = UBound(nArray)
nTemp1 = 0
nTemp2 = 0
For i = 2 to n - 3 step 2
nTemp1 = nTemp1 + (h * n)
Next i
For i = 1 to n - 2 step 2
nTemp2 = nTemp2 + (h * n)
Next i
GetNum = (d / 3) * (nArray[0] + nArray[n] + 2 * nTemp1 + 4 * nTemp2)
End Function


PS: I momentarily forgot how to do that - my mind is constantly trying to switch between VB and C++... can get confusing some times ;->
Best Regards and many Thanks!
Michael G. Bronner X-)

"He was a wise man who invented beer." Plato
 
MichaelRed / MichaelBronner :
Could you please give me the exact code wherein i have to enter the values manually by using an inputbox and the values could be any number ... This is the best that you could give me. i really need it badly...thanx!
 
OK, here'y'are ;-> :

Private Sub Form_Load()
Dim nArray(0 To 100) As Float

Call InputValues(nArray)
fResult = GetNum(nArray)
[do what you want with fResult here]
End Sub

Private Sub InputValues(nArray As Float)
Dim i As Integer
For i = 0 To UBound(nArray)-1
nArray = InputBox("Please enter the " & i & ". value:","Input Values for Array",Default)
Next i
End Sub

Private Function GetNum(nArray As Integer) As Float
Dim n As Integer
Dim nResult as Integer
Dim nTemp1 As Integer
Dim nTemp2 As Integer

n = UBound(nArray)
nTemp1 = 0
nTemp2 = 0
For i = 2 to n - 3 step 2
nTemp1 = nTemp1 + (h * n)
Next i
For i = 1 to n - 2 step 2
nTemp2 = nTemp2 + (h * n)
Next i
GetNum = (d / 3) * (nArray[0] + nArray[n] + 2 * nTemp1 + 4 * nTemp2)
End Function


The rest of the form you will have to create with labels, textboxes, or whatever you need. But this should get you well on your way. Take Care,
Mike
 
i got an error here : user type not defined
Private Sub InputValues(nArray As Float)
 
Oops, there I go again, mixing up C++ and VB. Whereever I wrote Float, replace it with Double.

Sorry :)


<a href=&quot; target=&quot;top&quot;>
OnLineR
</a> Take Care,
Mike
 
add byref in front of nArray where this error occurs:


Private Sub InputValues(ByRef nArray As Double)

Private Function GetNum(byRef nArray As Double) As Double Take Care,
Mike
 
Here is the code something is still wrong,thanx!

Private Sub Form_Load()
Dim nArray(0 To 100) As Double

Call InputValues(nArray)
fResult = GetNum(nArray)
' [do what you want with fResult here]
End Sub

Private Sub InputValues(ByRef nArray As Double)
Dim i As Integer
For i = 0 To UBound(nArray) - 1
nArray = InputBox(&quot;Please enter the &quot; & i & &quot;. value:&quot;, &quot;Input Values for Array&quot;, Default)
Next i
End Sub

Private Function GetNum(ByRef nArray As Double) As Double
Dim n As Integer
Dim nResult As Integer
Dim nTemp1 As Integer
Dim nTemp2 As Integer

n = UBound(nArray)
nTemp1 = 0
nTemp2 = 0
For i = 2 To n - 3 Step 2
nTemp1 = nTemp1 + (h * n)
Next i
For i = 1 To n - 2 Step 2
nTemp2 = nTemp2 + (h * n)
Next i
GetNum = (d / 3) * (nArray[0] + nArray[n] + 2 * nTemp1 + 4 * nTemp2)
End Function
 
What is wrong? Does it return an error message, or is the result incorrect? Take Care,
Mike
 
There are a couple of things that may need changing. First off, the msgbox function returns integer values, not doubles (which is how you've dimensioned your nArray variable). In this case, I believe VB will do an implicit conversion to a double though. However, you need to specify which element of the array needs to receive the new value (either the integer to long thing, or this is what's giving you the byRef mismatch error). Try this:

Private Sub InputValues(ByRef nArray As Double)
Dim i As Integer
For i = 0 To UBound(nArray) - 1
nArray(i) = cdbl(InputBox(&quot;Please enter the &quot; & i & &quot;. value:&quot;, &quot;Input Values for Array&quot;, Default))
Next i
End Sub


I didn't really look at what you're trying to do, and I don't mean to be a &quot;naysayer&quot; but I suspect that you're going to find quite a few more problems with the code as you now have it, even with this change. Just a couple of suggestions... fResult needs to be dimensioned (and the same rules apply here as they do to nArray), and the ByRef statements are not necessary (the default in VB is byRef).

Good Luck.
 
I noticed you will also find errors wherever nArray is followed by brackets, e.g. nArray[]. You will need to change these to parentheses, e.g. nArray().

sorry about the missing (i) in the for loop... somehow it got omitted when iI submitted it (the forum thought it was TGML and made the rest italic). Take Care,
Mike
 
The Error with your code michaelbronner is that &quot;ByRef Argument Type Mismatch&quot;.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top