• Status: Solved
• Priority: Medium
• Security: Public
• Views: 152

# Using a step variable

I am make a program to measure the liquid in a cylindrical tank, laying horizontally. I can get it in inch increments, but I would like to get it in half inch and maybe quarter inches. Can I use a variable Step to do this? When I try a Step of .5, I get bad results. This code will work with a Command1, and Text1 set to multiline true. Variables are (L) length of tank (h)height of liquid(r)radius, I double it to get diameter(v)volume at ech interval.
Private Sub Command1_Click()
Dim ResultsArray() As Single 'Or whatever

Dim r As Integer
Dim L As Integer
Dim h As Integer
Dim v As Single
Dim TempStr As String
Dim Title As String
'then when you need to use the arrays:

r = 20
ReDim ResultsArray(1 To 2 * r)
l = 72
For h = 1 To 2 * r - 1
v = (r ^ 2 * (ArcCos((r - h) / r))) _
- (r - h) * (Sqr(2 * (r * h) - h ^ 2))
v = L * v / 231
v = Format(v, "######.##")
ResultsArray(h) = v

Next h

TempStr = ""
For h = 1 To r

TempStr = TempStr & h & Chr(9) & ResultsArray(h) & _
Chr(9) & Chr(9) & Chr(9) & _
h + r & Chr(9) & ResultsArray(h + r) & vbCrLf
Next h
Title = "This is a test"
Text1.Text = Title & vbCrLf & TempStr

End Sub

'Inverse Cosine
Function ArcCos(x As Double) As Double
ArcCos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)
End Function
0
Ginger16
• 10
• 9
1 Solution

Commented:
Hi
May be
Dim h as single '(or double)? And other variables.
Cheers
0

Commented:
Hi
Dim r As Single
Dim L As Single
Dim h As Single
Dim v As Single
Dim MyStep as Single
Dim TempStr As String
Dim Title As String
'then when you need to use the arrays:
MyStep = 0.25 '1/4 inches
r = 20.5
ReDim ResultsArray(1 To 2 * CInt(r/MyStep))
l = 72.2
For h = 1 To 2 * r - 1 Step MyStep
'.....
Works fine

Cheers
0

Commented:
BTW - why you exlude empty and full tank?
Better
ReDim ResultsArray(2 * CInt(r/MyStep))'zero based
For h = 0 To 2 * r Step MyStep
Cheers
0

Author Commented:
Ark, I tried, but my answers are still coming up wrong, I get the same answer four times in a row then two or three times in a row. Am I doing you mods right?
Private Sub Command1_Click()
Dim ResultsArray() As Single 'Or whatever

Dim r As Single
Dim L As Single
Dim h As Single
Dim v As Single
Dim MyStep As Single
Dim TempStr As String
Dim Title As String

MyStep = 0.25
r = 20
ReDim ResultsArray(1 To 2 * CInt(r / MyStep))
L = 72
For h = 1 To 2 * r - 1 Step MyStep
v = (r ^ 2 * (ArcCos((r - h) / r))) _
- (r - h) * (Sqr(2 * (r * h) - h ^ 2))
v = L * v / 231
v = Format(v, "######.##")
ResultsArray(h) = v

Next h

TempStr = ""
For h = 1 To r Step MyStep

TempStr = TempStr & h & Chr(9) & ResultsArray(h) & _
Chr(9) & Chr(9) & Chr(9) & _
h + r & Chr(9) & ResultsArray(h + r) & vbCrLf
Next h
Title = "This is a test"
Text1.Text = Title & vbCrLf & TempStr

End Sub

'Inverse Cosine
Function ArcCos(x As Double) As Double
ArcCos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)
End Function

0

Commented:
Hi
First, you display wrong results
In second For...Next you display ResultsArray(1) then ResultsArray(1.25) etc - they are the same. You need ResultsArray ((h-1)/MyStep+1).
Second - check your formula. It seems you write it for 1 inch step
What is "v=v*l/231" ?

Cheers
0

Commented:
Another one
You have wrote that for 1 inch everything is OK. But the most volume increasing per 1 inch must be near tank center. Your results don't show this. I just trying to find correct formula.
Cheers
0

Author Commented:
0

Author Commented:
The way it is set up is so that in this example, I would have answers 1 through 20 on the left side of the page, and 21 through 40 in the center of the page. Two columns basically. Is there another way to get the answers in columns? As for the v=v*L/231, that is converting square inches into gallons.
0

Author Commented:
That code may have been altered too many yimes. This just prints to the Form, it is right
Option Explicit
Private Sub Command1_Click()
Dim ResultsArray() As Single 'Or whatever
Dim d As Integer
Dim r As Single
Dim l As Single
Dim h As Single
Dim v As Single
'then when you need to use the arrays:
d = 48
r = d / 2
ReDim ResultsArray(1 To 2 * r)
l = 72
For h = 1 To 2 * r - 1
v = (r ^ 2 * (ArcCos((r - h) / r))) _
- (r - h) * (Sqr(2 * (r * h) - h ^ 2))
v = l * v / 231
v = Format(v, "#######")
ResultsArray(h) = v

Next h

For h = 1 To r
If h + r = d Then
ResultsArray(h + r) = Int(d * d * l * 0.0034)
End If
Print Spc(7); h; Chr(9); ResultsArray(h), , h + r; Spc(3); ResultsArray(h + r)
Next h

0

Commented:
Hi
Correct code
Private Sub Command1_Click()
Dim ResultsArray() As Single 'Or whatever

Dim r As Single
Dim r1 As Single
Dim L As Single
Dim h As Single
Dim v As Single
Dim MyStep As Single
Dim TempStr As String
Dim Title As String

MyStep = 0.25
r = 20
ReDim ResultsArray(1 To 2 * CInt(r / MyStep))
L = 72
For h = 1 To 2 * r - 1 Step MyStep
v = (r ^ 2 * (ArcCos((r - h) / r))) - (r - h) * (Sqr(2 * (r * h) - h ^ 2))
v = L * v / 231
v = Format(v, "######.##")
ResultsArray((h - 1) / MyStep + 1) = v

Next h

TempStr = ""
For h = 1 To r Step MyStep
TempStr = TempStr & h & Chr(9) & ResultsArray((h - 1) / MyStep + 1) & Chr(9) & Chr(9) & Chr(9) & h + r & Chr(9) & ResultsArray((h - 1) / MyStep + 1 + r/MyStep) & vbCrLf
Next h
Title = "This is a test"
Text1.Text = Title & vbCrLf & TempStr

End Sub

'Inverse Cosine
Function ArcCos(x As Double) As Double
ArcCos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)
End Function
0

Author Commented:
that worked. Still has problem in that last inch. I tried 0 to r  step MyStep, but got divide by zero error. Do you know how to make it go into four columns instead of two. That will be my next question.
0

Author Commented:
More help than I ever imagined I would get. Watch for more wierd questions.
0

Commented:
Hi
Private Function Arccos(ByVal x As Double) As Double
If Abs(x) <> 1 Then
Arccos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)
Else
Arccos = IIf(x = 1, 0, Atn(1) * 4)
End If
End Function
You need to change
ReDim ResultsArray(0 To 2 * CInt(r / MyStep)), If v > 0 Then v = Format(v, "######.##")
and all (h-1)/MyStep + 1 to h/MyStep
About four columns - if you still want to use Text box, try this
TempStr = ""
For h = 0 To r * 2 - 1 Step MyStep * 4
For i = 0 To 2
TempStr = TempStr & h + i * MyStep & Chr(9) & ResultsArray(h / MyStep + i) & Chr(9) & Chr(9)
Next i
TempStr = TempStr & h + i * MyStep & Chr(9) & ResultsArray(h / MyStep + i) & vbCrLf
Next h
Cheers
0

Author Commented:
That put the numbers going across the page as they increase in value, I need them to increase like 1 to 10 in the left column, 11 to 20 in second column, 20 to 30 in third column, 30 to 40 in fourth.
0

Commented:
Hi
It's here
Dim nColumns As Integer, nRows As Integer
nColumns = 4
nRows = UBound(ResultsArray) / nColumns
For h = 0 To r * 2 - 1 Step MyStep * nColumns
For i = 0 To nColumns - 2
TempStr = TempStr & h / nColumns + i * nRows * MyStep & Chr(9) & ResultsArray(h / (MyStep * nColumns) + i * nRows) & Chr(9) & Chr(9)
Next i
TempStr = TempStr & h / nColumns + i * nRows * MyStep & Chr(9) & ResultsArray(h / (MyStep * nColumns) + i * nRows) & vbCrLf
Next h
Cheers
0

Author Commented:
What am I doing wrong? Still coming out funky,
Option Explicit

Private Sub Command1_Click()
Dim ResultsArray() As Single

Dim i As Integer
Dim r As Single
Dim r1 As Single
Dim L As Single
Dim h As Single
Dim v As Single
Dim MyStep As Single
Dim TempStr As String
Dim Title As String
Dim nColumns As Integer
Dim nRows As Integer
nColumns = 4

MyStep = 0.5
r = 20
ReDim ResultsArray(0 To 2 * CInt(r / MyStep))
L = 72
For h = 1 To 2 * r - 1 Step MyStep
v = (r ^ 2 * (ArcCos((r - h) / r))) - (r - h) * (Sqr(2 * (r * h) - h ^ 2))
v = L * v / 231
If v > 0 Then
v = Format(v, "######.#")
End If
ResultsArray(h / MyStep) = v

Next h
TempStr = ""
nRows = UBound(ResultsArray) / nColumns
For h = 0 To r * 2 - 1 Step MyStep * nColumns
For i = 0 To nColumns - 2
TempStr = TempStr & h / nColumns + i * nRows * MyStep & Chr(9) & ResultsArray(h / (MyStep * nColumns) + i * nRows) & Chr(9) & Chr(9)
Next i
TempStr = TempStr & h / nColumns + i * nRows * MyStep & Chr(9) & ResultsArray(h / (MyStep * nColumns) + i * nRows) & Chr(9)
Next h
Title = "This is a test"
Text1.Text = Title & vbCrLf & TempStr

End Sub

'Inverse Cosine
Function ArcCos(ByVal x As Double) As Double
If Abs(x) <> 1 Then
ArcCos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)
Else
ArcCos = IIf(x = 1, 0, Atn(1) * 4)
End If
End Function

0

Commented:
Hi
TempStr = TempStr & h / nColumns + i * nRows * MyStep & Chr(9) & ResultsArray(h / (MyStep * nColumns) + i * nRows) & vbCrLf 'Last is vbCrLf, NOT Chr(9)
0

Author Commented:
Believe it or not, I actually figured that out without looking at you code a second time(although I should have). Thank you so much for the help. Do you want more points for the extra help, I kinda got carried away. If so, how do I do it?
0

Commented:
Hi
Glad to help you. About points - I need them <smile>, but your question wasn't very hard, so I thing you gave enough points. Anyway, it's your decision. If you want to give me (or smbd else) more points, you can ask a question named "Points to Ark" or "Points to somebody" and then accept any answer.

Cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.