Solved

Using a step variable

Posted on 2000-03-11
19
130 Views
Last Modified: 2010-05-02
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
Comment
Question by:Ginger16
  • 10
  • 9
19 Comments
 
LVL 27

Expert Comment

by:Ark
ID: 2608623
Hi
May be
Dim h as single '(or double)? And other variables.
Cheers
0
 
LVL 27

Expert Comment

by:Ark
ID: 2608630
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
 
LVL 27

Expert Comment

by:Ark
ID: 2608638
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 Comment

by:Ginger16
ID: 2608662
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
 
LVL 27

Expert Comment

by:Ark
ID: 2608704
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
 
LVL 27

Expert Comment

by:Ark
ID: 2608716
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 Comment

by:Ginger16
ID: 2608734
Adjusted points to 100
0
 

Author Comment

by:Ginger16
ID: 2608735
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 Comment

by:Ginger16
ID: 2608755
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 27

Accepted Solution

by:
Ark earned 100 total points
ID: 2608760
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 Comment

by:Ginger16
ID: 2608778
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 Comment

by:Ginger16
ID: 2608780
More help than I ever imagined I would get. Watch for more wierd questions.
0
 
LVL 27

Expert Comment

by:Ark
ID: 2608807
Hi
About division by zero:
Change your arccos function
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 Comment

by:Ginger16
ID: 2608847
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
 
LVL 27

Expert Comment

by:Ark
ID: 2608886
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 Comment

by:Ginger16
ID: 2608933
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
 
LVL 27

Expert Comment

by:Ark
ID: 2608955
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 Comment

by:Ginger16
ID: 2608971
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
 
LVL 27

Expert Comment

by:Ark
ID: 2608996
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now