PDA

View Full Version : Help with pseudocode and Visual Basic 2010



sportyguy1
24-08-2014, 05:13 PM
I am trying to make this Visual Basic code output a recursive answer, but it does nothing.

Public Class Form1



Function Y(ByVal s As String) As String



Dim x As Integer = s.Length
06


If x = 1 Then



Return (s)

Else



Return Y(Microsoft.VisualBasic.Right(s, x - 1) + Microsoft.VisualBasic.Left(s, 1))





End If

End Function



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim text As String = "BYTE"

Dim answer As String

answer = Y(text)

End Sub

End Class

pcuser42
24-08-2014, 05:29 PM
Public Class Form1
Function Y(ByVal s As String) As String
Dim x As Integer = s.Length
If x = 1 Then
Return (s)
Else
Return Y(Microsoft.VisualBasic.Right(s, x - 1) + Microsoft.VisualBasic.Left(s, 1))
End If
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim text As String = "BYTE"
Dim answer As String
answer = Y(text)
End Sub
End Class


I ran the code and got a StackOverflow exception. After stepping through the code, it seems as if all it's doing is moving the first letter to the end but not actually shortening the string, causing an infinite loop. What's the code supposed to do?

sportyguy1
24-08-2014, 06:59 PM
Did you see the marking schedule I attached. It looks like it's supposed to reverse the STRING that's entered, so in this case BYTE becomes ETYB.

pcuser42
24-08-2014, 07:46 PM
There isn't an attachment :confused:

sportyguy1
24-08-2014, 08:44 PM
https://docs.google.com/document/d/162eEw-4YI3CUKjLII9HN1QQyY7yVDQTB3y3A-aLR5aQ/edit?usp=sharing
https://docs.google.com/document/d/1apjfToyMreGLWdRIz5CxrOmFdHQdXs0ApA65fLSOD-Q/edit?usp=sharing

sportyguy1
24-08-2014, 08:46 PM
https://drive.google.com/file/d/0B89a1P30u9qWMWZ3XzFacVM5dnc/edit?usp=sharing
https://drive.google.com/file/d/0B89a1P30u9qWTjVjT25wTUd4Vjg/edit?usp=sharing

gary67
24-08-2014, 09:42 PM
I'm sure someone will give you some pointers but they won't do your homework for you. It won't be me I can't code to save myself.

sportyguy1
24-08-2014, 10:43 PM
it's okay I finally got a programmer to walk me through the pseudocode line by line and it sort of makes sense, but not.

He did say that most good programmers don't use recursion, but a lot of people are afraid of it.

inphinity
24-08-2014, 11:15 PM
He did say that most good programmers don't use recursion, but a lot of people are afraid of it.

Given the importance and capability of recursive procedures, any good programmer will be well-versed in recursion and will understand when it is (and isn't) the solution. It's one of the key procedural areas of computer science.

Your code given doesn't appear to have any termination condition, other than giving it an initial string of length 1. It needs to know when to stop.

beama
28-08-2014, 12:23 PM
Given the importance and capability of recursive procedures, any good programmer will be well-versed in recursion and will understand when it is (and isn't) the solution. It's one of the key procedural areas of computer science.

Your code given doesn't appear to have any termination condition, other than giving it an initial string of length 1. It needs to know when to stop.

and a bit of fault checking ie want happens if you have an empty string ie nothing entered