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.
And:
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.
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
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