Jump to content
MakeWebGames

VB.NET 2008 Floyds Triangle Update...


become

Recommended Posts

Hey, I would consider myself to be new to the forum, posting wise, however i have been hovering around helping a few people out via PM and reading through certain posts which interest me :)

I study visual basic 2008 in education and i happened to fall upon Floyd's Triangle (http://en.wikipedia.org/wiki/Floyd%27s_triangle) and thought it would be an achievement if i were to alter the code shown in the following link for it to have a more appealing UI, in terms of spacing between certain characters and displaying errors messages where appropriate - http://en.wikipedia.org/wiki/Visual_Basic_.NET (bottom of the page, roughly 7/8 down)

The reason im posting this up to so you can learn a few tips, if you are studying vb.net and i would like to share with the community a sample of code which i managed to change from wikipedia to work with vb.net 2008 rather than other variations.

You are able to freely download vb.net 2008 completely free from the microsoft web page (http://www.microsoft.com/express/Downloads/#2010-Visual-Basic) Visual Studio 2008 Express.

Please comment below on what you think its good/effective or what needs improving on, otherwise i hope you enjoyed my contribution. Screenshots are below and code is presented below to.

NOTE: I wouldn't consider this as 100% mine as the majority of the code i found on wikipedia.org so therefore I'd just like to point out that i only did slight alterations and added in a few (not many) notes for a programmer/pseudo code user to read and hopefully understand. To be completely honest with you, im not too sure on what everything does, but i hope this inspires some to learn and look into to hopefully make clearer to others :)

 

Module Program
   Sub Main()
       Dim rows As Integer
       'Input validation.
       Do Until Integer.TryParse(ReadLine("Enter a value for how many rows to be displayed: "), rows) AndAlso rows >= 1
           If rows < 1 Then
               Console.WriteLine("Warning: You cannot have less than 1 row")
           End If
           Console.WriteLine("Allowed range is 1 and {0}", Integer.MaxValue)
           Console.ReadLine()
       Loop
       'Output of Floyd's Triangle
       Dim current = 1
       For row = 1 To rows
           For column = 1 To row
               Console.Write("{0, -2}", current & " ") 'set spacing for each displayed number
               current += 1 'set increase for each integar
           Next
           Console.ReadLine()
       Next
   End Sub

   Function ReadLine(Optional ByVal prompt As String = Nothing) As String
       If prompt IsNot Nothing Then
           Console.Write(prompt)
       End If
       Return Console.ReadLine()
   End Function
End Module

 

I have attached a comparison screenshot of both original and editied codes so you can see the editied/added pieces of code/information. Again, its not alot in comparison to whats there, but it seemed to fix a few errors i had with the original code. No, i don't think this would come in useful to most people, but it's been said you should be able to code Floyd's Triangle with a language to understand the basic comprehension and understanding of it.

Thanks for taking an interest in this post,

Many thanks,

Become.

Comparison.thumb.jpg.c6b5f0e018859ddfdf5ef808fa567bab.jpg

Example.thumb.jpg.d94ecb2cc421151fa124b04edbb01b08.jpg

Edited by become
  • Like 1
Link to comment
Share on other sites

One remark, if your triangle has int.max rows... it will certainly fail (actually even before) as you well overflow the max value of a int before that much rows.

Then a simple question, why do a readline after each row? And not a readkey at the end if you want to avoid to loose the output while debugging?

Link to comment
Share on other sites

True, thanks for pointing that out a_bertrand, however im still novice. If you would take a quick look at the Comparison.jpg ive attached you will see the changes i have made to the original code. However, thank you greatly for pointing that out and i shall look into readkey ect and take into account what you have given me and i shall work on it to hopefully improve it :). Thanks though, i do appreciate the feedback and support, but please remember, im still novice :).

Thanks,

Become

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...