-
Apr 15th, 2014, 05:46 AM
#1
Thread Starter
New Member
Program For Geometric Progression
Hello! I would really appreciate any advice on my code - I have to make a program for Geometric Progression. Here's the code:
Code:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub cmd_compute_Click()
Dim x, n, num As Integer
Dim r As Single
x = TextBox1.Text
r = TextBox2.Text
num = TextBox3.Text
ListBox1.AddItem("n" & vbTab & "x")
ListBox1.AddItem("___________")
n = 1
Do
x = x * r
ListBox1.AddItem(n & vbTab & x)
n = n + 1
Loop Until n = num + 1
End Sub
Private Sub TextBox4_TextChanged(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
End Sub
End Class
-
Apr 15th, 2014, 02:38 PM
#2
Re: Program For Geometric Progression
Advice on your code... hmm... OK.
Form1_Load, Label1_Click, TextBox4_TextChanged and Button1_Click Subs have no code in them so could be removed.
Perhaps you want to clear ListBox1 in the cmd_compute_Click Sub before adding a new progression to it.
I assume you'll need to have something call the cmd_compute_Click() sub, which you are not showing.
You probably want to have some limit checks on the textbox inputs, i.e. you probably don't want num to be millions or more.
-
Apr 29th, 2014, 08:14 PM
#3
Addicted Member
Re: Program For Geometric Progression
This is a simple way to do it:
Code:
Option Explicit On
Option Strict On
Class Form1
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Dim a, x, r, n As Integer
x = CDec(TextBox1.Text)
n = CDec(TextBox3.Text)
Dim total As Decimal
ListBox1.Items.Add("X" & vbTab & vbTab & "R")
ListBox1.Items.Add("-----------------------------------------------------")
a = 1
Do While a <= n
Select Case a
Case Is = 1
r = 1
Case Else
r = CDec(TextBox2.Text)
End Select
ListBox1.Items.Add(x & vbTab & "x" & vbTab & r & vbTab & "=" & vbTab & (x * r))
x = x * r
a += 1
total += x
Loop
Label4.Text = total.ToString
End Sub
End Class
Last edited by Atenk; May 2nd, 2014 at 05:54 AM.
-
May 2nd, 2014, 08:49 AM
#4
Addicted Member
Re: Program For Geometric Progression
This is an enhancement of the program:
1- I declared all variables as decimal to let the user deal with decimal sequence and decimal ratio.
2 - I added a running total column: that will help the user check his answer when he calcultes manually the term at any rank(3rd term, last term, 7th term, and so on.)
Code:
Option Explicit On
Option Strict On
Class Form1
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Dim a, x, y, r, n As Decimal
x = CDec(TextBox1.Text)
r = CDec(TextBox2.Text)
n = CDec(TextBox3.Text)
y = x
Dim total As Decimal
ListBox1.Items.Add("X" & vbTab & vbTab & "R" & vbTab & vbTab & vbTab & vbTab & "Total")
ListBox1.Items.Add("--------------------------------------------------")
a = 1
Do While a <= n
Select Case a
Case Is = 1
r = 1
Case Else
r = CDec(TextBox2.Text)
End Select
total += CDec(y * (r ^ (a - 1)))
ListBox1.Items.Add(x & vbTab & "x" & vbTab & r & vbTab & "=" & vbTab & (x * r) & vbTab & vbTab & total)
x = x * r
a += 1
Loop
End Sub
End Class
Last edited by Atenk; May 2nd, 2014 at 08:57 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|