Program This

Discussion in 'General Off-Topic Chat' started by jonthedit, Sep 22, 2013.

  1. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    So I've been going through tons of coding e-books that contain various exercises.
    This is intended to be coded in VB 2005, but honestly I couldn't care less.
    I'm personally coding this in VB 2005. I'm almost finished, but I would like to see what other programmers come up with to compare to mine. I'm a bit lost when it comes to how arrays are coded and why... So annotating your work would greatly help me.

    The assignment doesn't say this, but this is what I would like to see implemented
    - Two forms, the first one asking Display in Age or Birthday
    - The second form asking Ascending or Descending
    Finally, the code would write the names and ages into the Intermediate window/Output in VB2005 Express.


    a.
    You have been asked to write a program that accepts a series of names and either ages or birth dates, sorts them in either ascending or descending order, and then displays them.
    Sorting Data
    The Bubble Sort is a common way to sort data. Heres how it works:

    Enter data into an array

    Compare the value of the first element to the value of the second element

    If they are out of order, swap their positions in the array

    Note: this requires moving one value to a temporary holding variable so its value isn't lost when it is overwritten during the swap.

    Compare the second elements value (this will be the new second element if positions were swapped in the previous step) to the third elements value

    If they are out of order, swap their positions in the array

    Compare the third elements value (this will be the new third element if positions were swapped in the previous step) to the fourth elements value

    If they are out of order, swap their positions in the array

    Continue this way until the end of the array is reached

    This process will have improved the order. By performing the process again the order will improve even more. The program must loop through the process as many times as necessary until all the elements in the array are in the desired order.
    You will find that you must make use of many of the concepts and procedures youve learned so far, such as arrays, subs, functions, loops, and operators.
    b.
    Create a flowchart and pseudo-code for the program. I don't care about this
    c.
    Code the program.

    //

    Here is my code, plus some screenshots of what the forms look like.
    Code:
    Public Class Form2
        Dim ages As Integer
        Dim userinput As String
        Dim Count1 As Integer
        Dim Count2 As Integer
        Dim Names(,) As String
        Dim TempV(1, 1) As String
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            userinput = InputBox("How many people are you entering?", "Enter People")
            If IsNumeric(userinput) = True Then
                ages = CInt(userinput)
                ReDim Names(ages, 2)
                For Count1 = 1 To userinput
                    Names(Count1, 1) = InputBox("Enter name #" & Count1 & ":", "Name Entry")
                    Names(Count1, 2) = InputBox("Enter age #" & Count1 & ":", "Name Entry")
                Next
                Count2 = 1
                Do While Count2 < ages
                    Count1 = 1
                    Do While Count1 < ages
                        If RadioButtonUp.Checked = False Then
     
                            'Ascend
                            If CInt(Names(Count1 + 1, 2)) > CInt(Names(Count1, 2)) Then
                                TempV(1, 0) = Names(Count1, 1)
                                TempV(1, 1) = Names(Count1, 2)
                                Names(Count1, 1) = Names(Count1 + 1, 1)
                                Names(Count1, 2) = Names(Count1 + 1, 2)
                                Names(Count1, 1) = TempV(1, 0)
                                Names(Count1, 2) = TempV(1, 1)
                            End If
                        ElseIf RadioButtonDown.Checked = True Then
                            'DN
                            If CInt(Names(Count1 + 1, 2)) < CInt(Names(Count1, 2)) Then
                                TempV(1, 0) = Names(Count1, 1)
                                TempV(1, 1) = Names(Count1, 2)
                                Names(Count1, 1) = Names(Count1 + 1, 1)
                                Names(Count1, 2) = Names(Count1 + 1, 2)
                                Names(Count1, 1) = TempV(1, 0)
                                Names(Count1, 2) = TempV(1, 1)
                            End If
                        End If
                        Count1 = Count1 + 1
                    Loop
                    Count2 = Count2 + 1
                Loop
                For Count1 = 1 To ages
                    Debug.WriteLine(Names(Count1, 1) & ", " & Names(Count1, 2))
                Next
            Else
                MsgBox("Error. Runtime exception.", 16, "YOU NEED TO READ")
                End
            End If
        End Sub
    End Class
    Form1.png Form2.png

    And:
    Code:
    Public Class Form1
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Close()
        End Sub
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If RadioButton1.Checked Then
                Me.Hide()
                Form2.Show()
            ElseIf RadioButton2.Checked Then
                Me.Hide()
                Form2.Show()
            End If
     
        End Sub
    End Class
     
  2. Foxi4

    Foxi4 On the hunt...

    pip Reporter
    23,537
    21,496
    Sep 13, 2009
    Poland
    Gaming Grotto
    Arrays are simply a good way to organize your variables when you know that you'll need several variables of the same type.

    For example, if you are going to store 50 birthday values then you can do it like a busy idiot and declare Birthday_1, Birthday_2 and so on until you have 50 or... you could just declare an array Birthday with 50 items.

    Additionally, Arrays make it easier to scan through the variables which is useful in automating the program and cuts down on the number of necessary lines. Looping through an Array is infinitely easier than writing down 50 items manually.
     
    jonthedit likes this.
  3. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh


    Alright, so let's say I have two forms.

    form1 has three timers.
    in form2, I want to be able to execute endform1() which will be listed in Module1.

    sub endform1()
    timer1.checked = false
    timer2.checked = false
    timer3.checked = false
    form1.hide()
    end sub

    Yet Module1 is confused because timer1 is only declared in form1...
    How do I 'call' upon something to kill stuff in form1?
     
  4. Foxi4

    Foxi4 On the hunt...

    pip Reporter
    23,537
    21,496
    Sep 13, 2009
    Poland
    Gaming Grotto
    I'm actually not very adept in using Visual Basic... or Basic in general for that matter - I generally opt for C/C++ in my project, so it's not a question I can answer - I'm sure there will be someone who can though. ;)