I was creating a basic number square grid using iteration (only using FOR loops). Unfortunately, I faced some issues with the program not presenting the number square in the preferred format I was trying to present it in - 1 to 10 above and on the side of the number square.
Without using arrays and only FOR loops, could someone help me find a method to present the number square in my preferred format:
Console.WriteLine("[] 1 2 3 4 5 6 7 8 9 10")
Console.WriteLine("1 / / / / / / / / / /")
Console.WriteLine("2 / / / / / / / / / /")
Console.WriteLine("3 / / / / / / / / / /")
Console.WriteLine("4 / / / / / / / / / /")
Console.WriteLine("5 / / / / / / / / / /")
Console.WriteLine("6 / / / / / / / / / /")
Console.WriteLine("7 / / / / / / / / / /")
Console.WriteLine("8 / / / / / / / / / /")
Console.WriteLine("9 / / / / / / / / / /")
Console.WriteLine("5 / / / / / / / / / /")
Sub Main()
Dim output As Integer
For count1 = 0 To 10
Console.Write(Format(output, " 000") & " ")
Next
Console.WriteLine()
Console.WriteLine()
For count3 = 1 To 10
For count4 = 0 To 10
output = count3 * count4
Console.Write(Format(output, " 000") & " ")
Next
Console.WriteLine()
Next
Console.ReadKey()
End Sub
Related
Assuming I have the following PostgresSQL table locations name/longitude/latitude:
name | longitude | latitude
-----------------------------------------
A | 14.02023923239 | 13.020239232393
B | 23.29328403231 | 20.203923847782
C | 8.02392784729 | 50.302398462732
D | 28.23828482848 | 29.845102045853
E | 32.20328328849 | 39.923828328782
How can I select the rows that are in a radius of (for example) 10 kilometers from a starting point longitude 13.99999999999 and latitude 12.99999999999?
I accept any answer that gives me a concrete SQL statement.
You need to transform longitude and latitude into a different coordinate system that maps lenghts accurately.
Install the PostGIS extension and use st_transform to do that transformation. Then use st_dwithin to find the matching rows.
It's an easy task if you have additional module earthdistance installed (or if you can install it):
select *
from mytable
where (point(lon, lat) <#> point(14, 13)) <= 10 / 1.609
<#> gives you a distance in miles, so we need to convert the kilometers to miles on the right operand of the comparison.
Accuracy might vary (the module assumes that Earth is a perfect sphere) - if you need something more accurate, you want to use PostGIS.
Thanks guys. I found this stored procedure on some website (forgot the link).
It works really well implementing the Haversine formula:
CREATE OR REPLACE FUNCTION calculate_distance(lat1 float, lon1 float, lat2 float, lon2 float, units varchar)
RETURNS float AS $dist$
DECLARE
dist float = 0;
radlat1 float;
radlat2 float;
theta float;
radtheta float;
BEGIN
IF lat1 = lat2 OR lon1 = lon2
THEN RETURN dist;
ELSE
radlat1 = pi() * lat1 / 180;
radlat2 = pi() * lat2 / 180;
theta = lon1 - lon2;
radtheta = pi() * theta / 180;
dist = sin(radlat1) * sin(radlat2) + cos(radlat1) * cos(radlat2) * cos(radtheta);
IF dist > 1 THEN dist = 1; END IF;
dist = acos(dist);
dist = dist * 180 / pi();
dist = dist * 60 * 1.1515;
IF units = 'K' THEN dist = dist * 1.609344; END IF;
IF units = 'N' THEN dist = dist * 0.8684; END IF;
RETURN dist;
END IF;
END;
$dist$ LANGUAGE plpgsql;
With that stored procedure, I was able to build a SQL select:
select
*
from
locations
where
calculate_distance(latitude, longitude, 13.99999999999, 12.99999999999, 'K') < 2
I have variables populated throughout my program and on the last form I'm trying to display a pie chart that shows the portions of each expense in respect to the total expenses. The code I have right now is as follows:
Sub Chart()
Chart1.Series("Expenses").ChartType = SeriesChartType.Pie
Chart1.Series("Expenses").Points.Add(GlobalVariables.cellphone / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.carinsurance / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.drappointments / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.grocery / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.healthinsurance / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.medications / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.misc / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.rent / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.therapysessions / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.travel / totalexpenses * 100)
'Chart1.Width = 700
Chart1.Legends.Add("Expenses")
Chart1.Legends("Expenses").LegendStyle = LegendStyle.Table
End Sub
When I run the program and include the call to the sub the chart does not show at all and the legend still only shows "Series1." What am I doing wrong? How can I make this work?
By default the 1st series is 'Series1' and the pie chart is taking the blank data of this series. You need to delete it if you would use your own series. Add this code as the 1st line :
Chart1.Series.clear
I was simply looking for a way to get cube roots in vb.net. The consensus online is to use the formula:
<number> ^ (1 / 3)
I tried punching a few of these into the immediate window and here's what I get:
?1 ^ (1 / 3)
1.0
?8 ^ (1 / 3)
2.0
?27 ^ (1 / 3)
3.0
?64 ^ (1 / 3)
3.9999999999999996
Wait a minute.. Shouldn't the answer be 4.0? What happened? 4 * 4 * 4 = 64, not 3.9999999999999996 * 3.9999999999999996 * 3.9999999999999996 = 64. I'm usually good with math problems but I've spent too much time with this and I could use some help. I'm not as interested in finding out why this failed as much as I am interested in how to make this work given the number 64 and trying to get the cube root to equal 4.
This works for me:
Private Function CubedRoot(ByVal dNum As Double) As Double
Return CType(CType(dNum ^ (1 / 3), Decimal), Double)
End Function
I am in the process of converting Mathcad code into VBA and am trying to figure out how to replicate the While loop, which asks the program to run the loop while TGuess < 0. At the end of the loop is an if statement to break the loop if sGuess>1/1.4 (I would attach a picture, but my reputation does not allow me to).
I have written this code in VBA, but am wondering if including the sGuess variable in the original While statement is correct, or if it could influence the output of the loop:
While TGuess < 0 And sGuess <= 1 / 1.4
kterm = (kj ^ (1 / 6)) / 26 'k term in the numerator of depth equation
epw = 3 / 5
FDepth = ((kterm * RainInt * L * CF) / sGuess ^ 0.5) ^ epw
tflow = UW_Wat * g * sGuess * FDepth 'Calc Flow Shear Stress
pflow = 7.853 * UW_Wat * (tflow / UW_Wat) ^ (3 / 2)
TGuess = pflow - pcrit 'Recalc TGuess as E-P
sGuess = sGuess + SlopeInc 'Calc new stable slope
Wend
Any input would be appreciated.
To mitigate your concern, it might be better to replace the while...wend loop with a Do While ... Loop block. You can then put your break condition where you'd have it in the corresponding Mathcad code by using something along the lines of
If sGuess > 1/1.4 Then
Exit Do
End If
Alright, I'm trying to understand follow sets and I think I got it except for one thing:
X -> a X
X -> b X
X -> epsilon
Following the rules of this page, FOLLOW(X) should contains $, the end of file character (rule 1). Then, following rule 3, FOLLOW(X) contains everything of FOLLOW(X) which makes my brain melt.
To me, intuitively, FOLLOW(X) should be {a,b,$}, but trying this example in kfg Edit gives me only {$}. Why?
FOLLOW(X) trivially is a subset of itself, so rule 3 does not contribute anything when applied to right-recursive productions.
While this is easily approached descriptively, your difficulty may originate from thinking about algorithms to compute. For computing FOLLOW sets, you could iteratively fill them according to the rules up to saturation. Then you don't need do anything for the trivial case either.
There is however no rule that gets a or b into FOLLOW(X), and I can't see any reason to expect them in FOLLOW(X). The grammar is simple enough to imagine the complete set of syntax trees that it can generate:
X
/|
X / X
/| / /|
X / X / / X
/| / /| / / /|
X / X / / X / / / X
/| / /| / / /| / / / /|
X / X / / X / / / X / / / / X
/| / /| / / /| / / / /| / / / / /|
X / X / / X / / / X / / / / X / / / / / X
| / | / / | / / / | / / / / | / / / / / |
ε α ε α α ε α α α ε α α α α ε α α α α α ε ...
( for α ∊ {a, b} )
They only ever allow X to the very right, so there is no way to have X followed by a or b.