Quadratic Equation Visual Basic 2010+ - visual-studio-2010

I have to write a program which calculate quadratic equation and find its roots. The roots must be displayed via MsgBox-es, and the variables A, B and C must be entered via InputBox-es. For now I have written this, but it somehow doesn't work and I can't figure out why.
**I'm new to Visual Basic ..
Public Class Form1
Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim Det As Double
Dim x1 As Double
Dim x2 As Double
Private Sub txtA_Click(sender As Object, e As EventArgs) Handles txtA.Click
txtA.Text = InputBox("Please, enter value of the variable A.", "Enter A")
End Sub
Private Sub txtB_Click(sender As Object, e As EventArgs) Handles txtB.Click
txtB.Text = InputBox("Please, enter value of the variable B.", "Enter B")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub cmdCalculate_Click(sender As Object, e As EventArgs) Handles cmdCalculate.Click
A = Val(txtA.Text)
B = Val(txtB.Text)
C = Val(txtC.Text)
Det = B ^ 2 + 4 * A * C
If Det > 0 Then
x1 = (-B + Math.Sqrt(Det)) / (2 * A)
x2 = (-B - Math.Sqrt(Det)) / (2 * A)
MsgBox("The roots are " + x1 + " and " + x2 + " ! ", 64, "2 Roots")
ElseIf Det = 0 Then
x1 = -B / (2 * A)
MsgBox("The roots are " + x1 + " ! ", 64, "1 Double Root")
ElseIf Det < 0 Then
MsgBox("No roots ! ", 64, "No Roots")
End If
End Sub
Private Sub txtC_Click(sender As Object, e As EventArgs) Handles txtC.Click
txtC.Text = InputBox("Please, enter value of the variable C.", "Enter C")
End Sub
Private Sub cmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
End
End Sub
Private Sub cmdClear_Click(sender As Object, e As EventArgs) Handles cmdClear.Click
txtA.Text = ""
txtB.Text = ""
txtC.Text = ""
End Sub
End Class
http://imgur.com/hIcDxFv <-- Screen view

Oh, the problem was just because the x1 and x2 must be in Str()..
If Det > 0 Then
x1 = (-B + Math.Sqrt(Det)) / (2 * A)
x2 = (-B - Math.Sqrt(Det)) / (2 * A)
MsgBox("The roots are " + Str(x1) + " and " + Str(x2) + " ! ", 64, "2 Roots")
ElseIf Det = 0 Then
x = -B / (2 * A)
MsgBox("The roots are " + Str(x) + " ! ", 64, "1 Double Root")

Related

Visual Studio - Printing "X" with Coordinates

I need your help with the coordinates. What I would like to happen is to print an "X" after the given coordinates. Example: The given coordinates for x-axis is 2 and y-axis is 2
the output will be:
So basically, 2 "#" on the top and 2 "#" on the left, then it will print the letter "X"
Dim d As String = ""
For i = 0 To NumericUpDownX.Value
For j = 0 To NumericUpDownY.Value
d = d & "#"
Next
d = d & vbNewLine
Next
output.Text = d
I was able to print the # but I can't seem to figure out how to put the "X" there.
I'd do it like this with the String constructor and PadLeft:
Dim d As New System.Text.StringBuilder
For y = 0 To NumericUpDownY.Value
If y < NumericUpDownY.Value Then
d.AppendLine(New String("#", NumericUpDownX.Value + 1))
Else
d.AppendLine("X".PadLeft(NumericUpDownX.Value + 1, "#"))
End If
Next
output.Text = d.ToString
If you want something more inline with what you were originally doing, then:
Dim d As String = ""
For y = 0 To NumericUpDownY.Value
For x = 0 To NumericUpDownX.Value
If y = NumericUpDownY.Value AndAlso x = NumericUpDownX.Value Then
d = d & "X"
Else
d = d & "#"
End If
Next
d = d & vbCrLf
Next
output.Text = d

Visual Studio - Printing Element Of Array using Timer Control

I am creating a program that prints the element of array one at a time (from top left to bottom right) using the timer control, however, it's not printing the elements one at a time, it prints all of them at the same time. I need help, below is a sample screenshot and my code so far.
Public Class SymbolDrawFRM
Private symbol(10, 10) As String
Sub Drawing()
Dim s As String = ""
For i = 1 To rowNUD.Value
For j = 1 To columnNUD.Value
s = s & symbol(i, j) & # & " "
Next
s = s & vbCrLf
Next
outputTBX.Text = s
End Sub
Private Sub startStopBTN_Click(sender As Object, e As EventArgs) Handles startStopBTN.Click
Timer.Start()
End Sub
Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
Drawing()
End Sub
End Class
Ignoring the Array, and only using the NumericUpDown controls:
Private Sub startStop_Click(sender As Object, e As EventArgs) Handles startStop.Click
Timer1.Interval = 500
Timer1.Enabled = Not Timer1.Enabled
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Static x As Integer
Static y As Integer
If x = 0 And y = 0 Then
outputTBX.Clear()
End If
outputTBX.AppendText("# ")
x = x + 1
If x = columnNUD.Value Then
x = 0
outputTBX.AppendText(vbCrLf)
y = y + 1
If y = rowNUD.Value Then
x = 0
y = 0
Timer1.Stop()
MessageBox.Show("Done!")
End If
End If
End Sub

Quick sort in Visual Basic

I tried to make a quick-sort in VB2015, however when I run it, the values don't sort fully (however it does almost sort). I'm fairly sure that the problem has something to do with the two recurring lines.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
numbers = TextBox1.Text.Split()
Dim tempstring As String
Form2.Show()
tempstring = ""
quicksort(numbers, numbers.Length() - 1, 0)
For Each a As String In numbers
tempstring = tempstring + a + " "
Next
TextBox2.Text = tempstring
Form2.Show()
Form2.Chart1.Series(0).Points.DataBindY(numbers)
End Sub
Public Sub quicksort(list As Array, high As Integer, low As Integer)
MessageBox.Show(Str(high) + " " + Str(low))
ListView1.Items.Add(Str(high) + " " + Str(low))
Dim i As Integer
Dim pivot As Integer
'pivot = (high + low) / 2
pivot = high
If high > low + 1 And low >= 0 Then
i = low
For c = low + 1 To high
If Int(list(c)) <= Int(list(pivot)) Then
swap(list, c, i)
i = i + 1
End If
Next
quicksort(numbers, i - 2, low)
quicksort(numbers, high, i)
End If
End Sub
Public Sub swap(list As Array, x As Integer, y As Integer)
Dim temp As Integer
temp = list(x)
list(x) = list(y)
list(y) = temp
Form2.Chart1.Series(0).Points.DataBindY(numbers)
'pause()
End Sub
I know this is old, but somebody may come across this. Your SWAP sub needs to pass the parameters ByRef, or the swap is only taking place inside the sub's variables and not within your QuickSort routine.

Visual Basic 6 Game "21" not displaying MsgBox when necessary

Trying to make the game "21" in visual basic 6, i have everything done but MsgBox is not displaying when its Bust, or Blackjack. Any ideas?
Private Sub cmdCheckScore_Click()
lblPC1.Visible = True
lblPC2.Visible = True
lblPC3.Visible = True
End Sub
Private Sub cmdDrawCard_Click()
If lblDraw1.Caption = "" Then 'Draws 3 random numbers with 3 button clicks
intDraw1 = Int(Rnd * 10 + 1)
lblDraw1.Caption = intDraw1
ElseIf lblDraw2.Caption = "" Then
intDraw2 = Int(Rnd * 10 + 1)
lblDraw2.Caption = intDraw2
ElseIf lblDraw3.Caption = "" Then
intDraw3 = Int(Rnd * 10 + 1)
lblDraw3.Caption = intDraw3
End If
intPlayerScore = intPlayer1 + intPlayer2 + intDraw1 + intDraw2 + intDraw3
intComputerScore = intPC1 + intPC2 + intPC3
If intPlayerScore > 21 Then
MsgBox "Bust!"
ElseIf intPlayerScore = 21 Then
MsgBox "Blackjack!"
End If
End Sub
Private Sub Form_Load()
Randomize
Dim intPlayer1 As Integer
Dim intPlayer2 As Integer
Dim intPlayer3 As Integer
Dim intPC1 As Integer
Dim intPC2 As Integer
Dim intPC3 As Integer
Dim intDraw1 As Integer
Dim intDraw2 As Integer
Dim intDraw3 As Integer
Dim PlayerScore As Integer
Dim ComputerScore As Integer
intDraw1 = 0
intDraw2 = 0
intDraw3 = 0
intPlayer1 = Int(Rnd * 10 + 1)
intPlayer2 = Int(Rnd * 10 + 1)
lblPlayer1.Caption = intPlayer1
lblPlayer2.Caption = intPlayer2
intPC1 = Int(Rnd * 10 + 1)
intPC2 = Int(Rnd * 10 + 1)
intPC3 = Int(Rnd * 10 + 1)
lblPC1.Caption = intPC1
lblPC2.Caption = intPC2
lblPC3.Caption = intPC3
End Sub
I've been trying to figure this out for 2 hours, and still no solution.
I believe your variables are getting created out of scope, and therefore when the click code runs, they're all variants.
Move your declares out of Form_Load to above cmdCheckScore
Dim intPlayer1 As Integer 'at the top of your FORM
Dim intPlayer2 As Integer
Dim intPlayer3 As Integer
Dim intPC1 As Integer
Dim intPC2 As Integer
Dim intPC3 As Integer
Dim intDraw1 As Integer
Dim intDraw2 As Integer
Dim intDraw3 As Integer
Dim PlayerScore As Integer
Dim ComputerScore As Integer 'at the top of your FORM
Private Sub cmdCheckScore_Click()
Next, click off to the left and set a breakpoint on this line to verify the values are getting there!
intPlayerScore = intPlayer1 + intPlayer2 + intDraw1 + intDraw2 + intDraw3

Rotating a line in Visual Basic 6

I want to make a line rotate. I studied the pi and radians and I made my own algorithm (if I can call it like that). I don't like to use already-made code from the Internet. I want to discover them alone, but using logic. Here is the code:
Dim pi As Double
Dim a, b, c, d, e, x, y As Double
Dim speed, radius As Integer
Private Sub Form_Load()
pi = 3.14159265358979
speed = 1
radius = 600
End Sub
Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled = True Then
Command1.Caption = "Stop"
Else
Command1.Caption = "Start"
End If
End Sub
Private Sub Timer1_Timer()
ForeColor = vbWhite
timer1.interval=speed
Refresh
a = a + 2
b = Sin((a * pi) / 180)
c = Cos((a * pi) / 180)
y = radius * b
x = radius * c
Call Label1.Move(6240 + x, 4200 + y)
If Left(b, 1) = "-" Then
Label1.Caption = "---"
Else
Label1.Caption = "+++"
End If
If Left(c, 1) = "-" Then
Label1.Caption = Label1.Caption & " " & "---"
Else
Label1.Caption = Label1.Caption & " " & "+++"
End If
Line (3000 + x, 4200 + y)-(6240 + x, 4200 + y)
Line (3000, 4200)-(3000 + x, 4200 + y)
Line (6240, 4200)-(6240 + x, 4200 + y)
For d = 3000 To 6240
Line (d, 4200)-(3000 + x, 4200 + y)
Next
For e = 3000 + x To 6240 + x
Line (e, 4200 + y)-(6240, 4200)
Next
End Sub
I want to rotate the line on x-axis, not z (it appears to be z). I recalculated everything, but I don't see where is the problem. What would be an explained formula?
I believe you are after the following effect:
Option Explicit
Dim D As Long, S As Long, Y As Long
Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled = True Then
Command1.Caption = "Stop"
Else
Command1.Caption = "Start"
End If
End Sub
Private Sub Form_Load()
D = 1 'Start going down; change to 0 to start going up instead
Y = 100 'Mid point
End Sub
Private Sub Timer1_Timer()
If S Then
If S = 8 Then
S = 0
Else
S = S + 1
lblRate = "0"
Exit Sub
End If
End If
Refresh
If D Then
If Y < 200 Then
Select Case Y
Case Is < 20
'Begin to accelerate
Y = Y + 1
lblRate = "+1"
Case Is < 40
'Continue to accelerate
Y = Y + 2
lblRate = "+2"
Case Is < 160
'Set acceleration to peak
Y = Y + 3
lblRate = "+3"
Case Is < 180
'Begin to decelerate
Y = Y + 2
lblRate = "+2"
Case Else
'Continue to decelerate
Y = Y + 1
lblRate = "+1"
End Select
Else
'Stop and reverse direction
D = 0
S = 1
lblRate = "0"
End If
Else
If Y > 0 Then
Select Case Y
Case Is < 20
'Begin to accelerate
Y = Y - 1
lblRate = "-1"
Case Is < 40
'Continue to accelerate
Y = Y - 2
lblRate = "-2"
Case Is < 160
'Set acceleration to peak
Y = Y - 3
lblRate = "-3"
Case Is < 180
'Begin to decelerate
Y = Y - 2
lblRate = "-2"
Case Else
'Continue to decelerate
Y = Y - 1
lblRate = "-1"
End Select
Else
'Stop and reverse direction
D = 1
S = 1
End If
End If
Line (120, 100)-(120, Y)
End Sub
While not technically following a properly calculated curvature, it is more of a simplified version of a line rotating around the X-axis.
Also, make sure to use the Pixel scale mode, rather than Twips, for better drawing performance.

Resources