Solved

Using a step variable

Posted on 2000-03-11
19
138 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 28

Expert Comment

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

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 28

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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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 28

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 28

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
 
LVL 28

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 28

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 28

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 28

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 28

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

840 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