Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Recursion problem


Reply
 
Thread Tools Display Modes
  #1  
Old 03-10-2004, 04:07 AM
broadcast28 broadcast28 is offline
Newcomer
 
Join Date: Mar 2004
Posts: 3
Default Recursion problem


Hi
I need to Create a Visual Basic application that takes in a number of people and displays the number of handshakes exchanged.

The recursive formula is
HandShake(N) = (N-1) + HandShake(N-1)

And this is the function.
Function HandShake(n As Integer) As Integer

If n = 1 Then

' Number of handshakes between 1 person

HandShake = 0
Else
HandShake = (n - 1) + HandShake(n - 1)
End If

End Function

However I am unsure on how to create a form to call this function.


any suggestions would be appreciated
Reply With Quote
  #2  
Old 03-10-2004, 04:32 AM
Marathon Man's Avatar
Marathon Man Marathon Man is offline
Contributor
 
Join Date: Jan 2004
Location: The "B" Ark
Posts: 458
Default

Do you need to use recursion, the formula is handshakes = (n*(n-1))/2.
Reply With Quote
  #3  
Old 03-10-2004, 05:43 AM
an5w3r's Avatar
an5w3r an5w3r is offline
Senior Contributor
 
Join Date: Jan 2004
Location: Romania
Posts: 1,342
Default

Code:
Private Sub Command1_Click()
Function HandShake(n As Integer) As Integer

If n = 1 Then

' Number of handshakes between 1 person

HandShake = 0
Else
HandShake = (n - 1) + HandShake(n - 1)
End If

End Function

Private Sub Form_Load()
MsgBox HandShake(4)
End Sub
__________________
Fully customizable and easy to use .NET WPF charting library http://www.SoftwareGFX.com
Reply With Quote
  #4  
Old 03-10-2004, 05:43 AM
Andyh Andyh is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Southampton, UK
Posts: 335
Default

MarathonMan is correct but if you want to call the function from a form just have the call in a button click, the input can either be from a text box or from an input statement

Code:
 
Private Sub Command1_Click()
Dim n As Integer
n = CInt(Me.Text1)
'compares both answers
MsgBox CStr(HandShake(n)) + ":" + CStr((n * (n - 1)) / 2)

End Sub
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Map/Array/Room(x,y), amount of enemies... Hardest problem I've encountered so far... Kronikle66 Game Programming 16 09-30-2003 02:07 PM
ADO Recorset Problem coco286 Database and Reporting 1 09-28-2003 09:08 PM
Problem with VB6->Excel jluisfer Excel 7 09-07-2003 07:46 AM
Help with problem with Excel Automation within VB6 seidenstud Word, PowerPoint, Outlook, and Other Office Products 1 12-18-2002 11:44 AM
Timers jemerico General 7 05-30-2001 05:30 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->