PDA

View Full Version : Small coding problem



Peerzy
06-03-2006, 12:32 AM
Just doing some coding in VB (learning the ropes and what-not).

My program is based on World Cup (Football) groups.

Basically this is what the program looks like (GUI is a work in progress):

http://img224.imageshack.us/img224/951/prg6rp.jpg

You enter in the results and click the update button and it works out what teams get what points and the goal difference (goals scored - goals conceded).

I've entered some random test data here:

http://img224.imageshack.us/img224/2154/prg10tl.jpg

Now as you can see Paraguay should be 1st, Sweden 2nd, T&T 3rd and England 4th. How can i get my program to work out which one of those 4 labels is holding the highest information and then move the text in that label, along with the text in the goal difference label and the team name label to the top one. So basically the program would end up looking like this:

http://img224.imageshack.us/img224/795/prg21mo.jpg

(Excuse the piss poor paint editing.

Also note that "Paraguay" is it's own label, as is the goal difference ("2) and the points total ("9) so it involves swaping 3 labels depending on which has the highest value.

Attached is the program compiled for you to get the idea of how it works. Note only the a few teams work (first 3 or 4 and then England) the others don't put up the right teams for there groups and stuff so just use the first team's (Angola) group.

If needed i can upload the VB files as well.

ilw
06-03-2006, 03:19 PM
post some of the relevant bits of code you're using.
e.g. where you declare the variables
whatever code you're using to sort the teams into 1st, 2nd, 3rd
where you update the form to show the list of teams

Peerzy
06-03-2006, 03:32 PM
I'll upload the project files.

I havn't named any of the labels but have assigned a variable to the 6 labels that need sorting.


Dim point1 As Integer
Dim point2 As Integer
Dim point3 As Integer
Dim point4 As Integer
Dim team1 As String
Dim team2 As String
Dim team3 As String
Dim team4 As String
Dim goal1 As Integer
Dim goal2 As Integer
Dim goal3 As Integer
Dim goal4 As Integer

point1 = Label42.Caption
point2 = Label43.Caption
point3 = Label44.Caption
point4 = Label45.Caption
team1 = Label38.Caption
team2 = Label39.Caption
team3 = Label40.Caption
team4 = Label41.Caption
goal1 = Label62.Caption
goal2 = Label63.Caption
goal3 = Label64.Caption
goal4 = Label65.Caption

Those are the variable names and such. I can't post the code im using to sort them because thats the problem, i have none. Thats what i need.

Updating the form is done via a procedure called Team, which is called when the ComboBox holding the teams is either scrolled, validated, clicked or changed.

Attached is a rar with the project files (using VB6 btw).

Peerzy
06-05-2006, 12:22 AM
Anyone?

ilw
06-05-2006, 06:46 PM
Personally i'd store the data in arrays, ie have an array of integers for the points, goals and team names.

you could have a couple of nested loops and test for the biggest value of points, then the second biggest etc

e.g.


array of integers biggest[4] = 0 (i.e. an array of 4 integers all set to 0)
array of integers dummy[4] (an array of integers to copy the points data)

for n = 0 to 4
{
dummy[n] = points[n] // copy all to a dummy array that you can delete stuff from
}
for i = 0 to 3 //i basically is the ranking i.e. i=0 is the top team in the group and i = 3 the bottom
{
for j = 0 to 3
{
if dummy[j] > biggest[i] OR dummy[j] = biggest[i] AND goals bigger
{
biggest[i] = j //stores which team had the highest points
}
}
dummy[biggest[i]] = 0; // remove whichever team had the highest points from consideration next time
}
at the end biggest[0] should indicate the best team and biggest[3] the worst


Alternatively you can use a bubble sort algorithm.
pseudo code:

for i = 3 downto 0
{
for j = 0 to i-1
{
if points[j] > points[j+1] OR if (points are equal AND goals [j] > goals [j+1])
{
//if team j has more points than team j+1
//then swap them over using dummy variables to hold j's data temporarily
dummy_points = points[j]
dummy_name = name[j]
dummy_goals = goals[j]

points[j] = points[j+1]
points[j+1] = dummy_points

name[j] = name[j+1]
name[j+1] = dummy_name

goals[j] = goals[j+1]
goals[j+1] = dummy_goals
}
}

}

Then jsut print the arrays out from 0 to 3

http://en.wikipedia.org/wiki/Bubble_sort



Clear as mud?

Peerzy
06-05-2006, 07:14 PM
Personally i'd store the data in arrays, ie have an array of integers for the points, goals and team names.

you could have a couple of nested loops and test for the biggest value of points, then the second biggest etc

e.g.


array of integers biggest[4] = 0 (i.e. an array of 4 integers all set to 0)
array of integers dummy[4] (an array of integers to copy the points data)

for n = 0 to 4
{
dummy[n] = points[n] // copy all to a dummy array that you can delete stuff from
}
for i = 0 to 3 //i basically is the ranking i.e. i=0 is the top team in the group and i = 3 the bottom
{
for j = 0 to 3
{
if dummy[j] > biggest[i] OR dummy[j] = biggest[i] AND goals bigger
{
biggest[i] = j //stores which team had the highest points
}
}
dummy[biggest[i]] = 0; // remove whichever team had the highest points from consideration next time
}
at the end biggest[0] should indicate the best team and biggest[3] the worst


Alternatively you can use a bubble sort algorithm.
pseudo code:

for i = 3 downto 0
{
for j = 0 to i-1
{
if points[j] > points[j+1] OR if (points are equal AND goals [j] > goals [j+1])
{
//if team j has more points than team j+1
//then swap them over using dummy variables to hold j's data temporarily
dummy_points = points[j]
dummy_name = name[j]
dummy_goals = goals[j]

points[j] = points[j+1]
points[j+1] = dummy_points

name[j] = name[j+1]
name[j+1] = dummy_name

goals[j] = goals[j+1]
goals[j+1] = dummy_goals
}
}

}

Then jsut print the arrays out from 0 to 3

http://en.wikipedia.org/wiki/Bubble_sort



Clear as mud?

Clear as merky water:ermm:

Although i don't understand the code syntax you've used (im guessing it's java or C or something) i get your ideas and can understand what you mean so i'll try and VB it up.

I'll give it a go, it's better what i was trying. Loads of IF statements, but that crashed if certain values were the same and such.

Thanks.

ilw
06-05-2006, 07:27 PM
jsut realised that if you use the first algorithm then the dummy[biggest[i]] should be set to -1 not 0, otherwise teams with no points won't get listed.

Peerzy
06-05-2006, 07:29 PM
Im going for the bubble sort method. So far i have


Private Sub Sort()
group [0] = Label42.Caption
group [1] = Label43.Caption
group [2] = Label44.Caption
group [3] = Label45.Caption
End Sub

Which is my array, im just not understanding how i can convert your i and j stuff into VB syntax, i know what you mean with it but just can't think of how it would work.