I want to plot a graph in Vb.net. I have x and y values , x indicates cycles. After every 4 cycles i want to average out all 4 cycles' value and draw a line then in the 5th cycle's graph shows its real graph value and then when cycles reach the 8th cycle then once again average out the value from the starting and plot a straight line. How can I plot this graph?
Private _averageValue As Double
Public ReadOnly Property AverageValue() As Double
Get
Return _averageValue
End Get
End Property
Private Sub CalculateAverageValue()
Try
Dim sum As Double = 0
Dim cnt As Integer = 1
_Reading = ""
'loop to find the average
If _SeparatedValues.Count > 0 Then
For cnt = 1 To _SeparatedValues.Count
sum = sum + _SeparatedValues(cnt - 1)
_Reading = _Reading & " " & _SeparatedValues(cnt - 1)
Next
_Reading = _Reading.Trim
_averageValue = sum / (cnt - 1)
Else
_averageValue = 0
End If
Catch ex As Exception
End Try
End Sub
Related
In the following project euler program #56, Considering natural numbers of the form, a^b, where a, b < 100, what is the maximum digital sum?
so I wrote the following code:
Dim num As System.Numerics.BigInteger
Dim s As String
Dim sum As Integer
Dim record As Integer
For a = 2 To 99
For b = 1 To 99
num = a ^ b
s = num.ToString
For i = 0 To s.Length - 1
sum += CInt(s.Substring(i, 1))
Next
sum = 0
Next
Next
The answer I got from the program was not the correct answer, so I wrote the following code so I can see what numbers set a new high value and see if something is wrong.
If sum > record Then
record = sum
Console.WriteLine(a & "," & b)
End If
One of the answers was a=10 b= 81. Obviously that doesn't make sense, because that value is 1 + 81 "0" = 1, but watching the result of 10^81, was 999999999999999921281879895665782741935503249059183851809998224123064148429897728
I searched about the accuracy of BigInteger but couldn't find anything, is there something that I'm missing?
I have a form that has a field with Inventory On Hand called QOH. I have 30 fields, each one contains a daily requirement and there is one field for each day. The fields are D1 through D30. What I want to do is create a loop that deducts the requirements (D1 through D30) from the QOH until the QOH falls below zero. I am counting the number of days as it is looping y = y + 1 to come up with the number of days of coverage. I then will return the counted result into a field on the form
Private Sub PDS_AfterUpdate()
Dim w As Integer 'generate the appropriate field reference'
Dim y As Integer 'My counter (The Value) that I return to the form'
Dim z As Double 'Quantity On Hand QOH'
Dim a As String
Dim strFieldName As String
z = Me.P0 ' PO is the field that contains the QOH - Quantity On Hand
If z < 0 Then ' If the QOH is already below zero return a -1
y = -1
End If
If z >= 0 Then
Do Until z < 0
w = w + 1 'This is used to get the number to add to the field to get the correct field'
a = "D" & w ' I then add the number to the field which starts with D to get the field that I want
to pull the value from'
strFieldName = "me." & a
z = z - strFieldName '<--- This is where I am stuck how do I get the value from the field I am
referencing in order to subtract it from the QOH'
y = y + 1 'This is the counter that I return to the field in my form'
Loop
End If
Me.DOH = y ' the field that the result is passed to on the form'
End Sub
Try this:
Do Until z <= 0
w = w + 1
a = "D" & CStr(w)
z = z - Me(a).Value
y = y + 1
Loop
I am getting the "next without for" error. I checked other questions on this and looked for any open if statements or loops in my code, but could find none. I'm need an extra set of eyes to catch my error here.
I am trying to loop through this code and advance the torque value 3 times each times it gets to the 30th i.
'This is Holzer's method for finding the torsional natural frequency
Option Explicit
Sub TorsionalVibrationAnalysis_()
Dim n As Integer 'position along stucture
Dim m As Integer
Dim i As Long 'frequency to be used
Dim j As Variant 'moment of inertia
Dim k As Variant 'stiffness
Dim theta As Long 'angular displacement
Dim torque As ListRow 'torque
Dim lambda As Long 'ListRow 'omega^2
Dim w As Variant
Dim s As Long
'equations relating the displacement and torque
n = 1
Set j = Range("d2:f2").Value 'Range("d2:f2").Value
Set k = Range("d3:f3").Value
'initial value
Set w = Range("B1:B30").Value
For i = 1 To 30
'start at 40 and increment frequency by 20
w = 40 + (i - 1) * 20
lambda = w ^ 2
theta = 1
s = 1
Do While i = 30 & s <= 3
torque = lambda * j(1, s)
s = s + 1
End
m = n + 1
theta = theta - torque(i, n) / k(n)
torque(i, m) = torque(i, n) + lambda * j(m) * theta
If m = 4 & i < 30 Then
w(i) = 40 + (i - 1) * 20
lambda = w(i) ^ 2
ElseIf m = 4 & i >= 30 Then
Cells([d], [5+i]).display (i)
Cells([e], [5+i]).display (theta)
Cells([f], [5+i]).display (torque)
Else
End If
If m <> 4 Then
n = n + 1
End If
Next i
End Sub
You are trying to terminate your While with an End instead of Loop
Try changing your End to Loop in your Do While loop. I think you are terming the loop when you hit that End
Proper indentation makes the problem rather apparent.
You have:
For i = 1 To 30
'...
Do While i = 30 & s <= 3
'...
End
'...
If m = 4 & i < 30 Then
'...
ElseIf m = 4 & i >= 30 Then
'...
Else
End If
If m <> 4 Then
'...
End If
Next i
But run it through Rubberduck's Smart Indenter and you get:
For i = 1 To 30
'...
Do While i = 30 & s <= 3
'...
End
'...
If m = 4 & i < 30 Then
'...
ElseIf m = 4 & i >= 30 Then
'...
Else
End If
If m <> 4 Then
'...
End If
Next i
End Sub
Notice how the End other answers are pointing out, is clearly not delimiting the Do While loop.
The Next i is inside the Do While block, which isn't terminated - when the VBA compiler encounters that Next i, it doesn't know how it could possibly relate to any previously encountered For statement, and thus issues a "Next without For" compile error.
Use an indenter.
I am trying to plot a chart using vb chart. There are some X values missing, which I would like to leave as a blank in the series plot. i.e, The X values start from 0.695, 0.7, 0.705, and so on. But there might be some gaps between them (example: 0.74, 0.745, 1.71, 1.715), which I would like leave as gaps (i.e. 0.745 to 1.71).
I was able to create an array of empty points, if that helps. Below is the code for the same.
Dim interval As Double = 0.0
Dim empty(0) As Double
Dim decimalpart As Integer = 0
interval = freq1(1) - freq1(0)
If interval.ToString().IndexOf(".") = -1 Then
decimalpart = 0 'No decimal part
Else
decimalpart = interval.ToString().Substring(interval.ToString().IndexOf(".") + 1).Length 'To find the number of decimal part
End If
y = 0
For i As Integer = 0 To freq1.Length - 2 'Dont need to access the last data. It would be accessed in the previous loop
If Math.Round((freq1(i + 1) - freq1(i)), decimalpart) > interval Then
empty(y) = freq1(i) + interval
y += 1
ReDim Preserve empty(y)
While (empty(y - 1) + interval < freq1(i + 1))
empty(y) = empty(y - 1) + interval
y += 1
ReDim Preserve empty(y)
End While
End If
Next
ReDim Preserve empty(y - 1)
The above code finds the interval and see if the next value is withing the interval range. Else it would find the values incremented using the interval value. freq1() is the array containing the X-axis values. However, I am not sure on how to remove the X axis values using empty(). (Not sure, if this could be done using Chart.Series().EmptyPointStyle)
I would like to show the gap appearing in the plot of the chart series.
I am not going to pretend what the code you have presented is doing, but showing a gap in a line plot for empty points is accomplished by setting the DataPoint.IsEmpty property to True.
Here is a simple example.
Dim s As New Series
s.ChartType = SeriesChartType.FastLine
s.Color = Color.Black
s.BorderWidth = 2
' make some points
For x As Double = 0.5 To 4 Step 0.025
Dim dp As New DataPoint(x, (2.0 * x + 3))
' apply some filtering criteria to set some points to empty
If dp.XValue >= 1.2 AndAlso dp.XValue < 1.5 Then dp.IsEmpty = True
If dp.XValue >= 3.1 AndAlso dp.XValue < 3.4 Then dp.IsEmpty = True
s.Points.Add(dp)
Next
Chart1.Series.Clear()
Chart1.Legends.Clear()
Chart1.Series.Add(s)
You can read more about this topic: Using Empty Data Points in Chart Controls
There are three columns in my data grid control. X1= Debits, X2= Credits and X3= Available Balance (like a bank balance statement). The X1 & X2 receive the data's from database & X3 calculate the amount by the following code:
Dim rCount As Integer
rCount = ds1.Tables(0).Rows.Count
With DG_All
For x As Integer = 0 To rCount - 1
.Rows.Add()
.Rows(x).Cells(0).Value = ds1.Tables(0).Rows(x)(0) 'X1
.Rows(x).Cells(1).Value = ds1.Tables(0).Rows(x)(1) 'X2
If x = 0 Then 'If first row of grid
If .Rows(x).Cells(0).Value = 0 Then 'If X1=0 & X2<0
.Rows(x).Cells(2).Value = .Rows(x).Cells(1).Value
Else 'If X1<0 & X2=0
.Rows(x).Cells(2).Value = .Rows(x).Cells(0).Value
End If
Else 'If the grid has more then 1 rows
If .Rows(x).Cells(0).Value = 0 Then
.Rows(x).Cells(2).Value = .Rows(x - 1).Cells(2).Value + .Rows(x).Cells(1).Value
Else
.Rows(x).Cells(2).Value = .Rows(x - 1).Cells(2).Value - .Rows(x).Cells(0).Value
End If
End If
Next
End With
My question is, I need to use this calculation method in crystal report to populate the available balance field in a new column.
Please can anyone help me?
I am using VB.Net 2012 Ultimate
giving you the basic idea, change as per your fields and requrirement.
Local Numbervar rCount:="Take your rows count here";
Local Numbervar x;
Local Numbervar x1;
Local Numbervar x2;
For x := 0 To rCount - 1 Do
(
X1= "field from databaset" 'X1
X2= "field from databaset" 'X2
Write your If and Else if confitions here
)
Note: I would have written but don't know what is row value and other terms from your code