Your Ad Here Your Ad Here
Results 1 to 8 of 8

Thread: Small coding problem

  1. #1
    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):



    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:



    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:



    (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.
    Attached Files Attached Files
    Last edited by Peerzy; 06-03-2006 at 12:33 AM.


  2. Internet, Programming and Graphics   -   #2
    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

  3. Internet, Programming and Graphics   -   #3
    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.

    Code:
    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).
    Attached Files Attached Files


  4. Internet, Programming and Graphics   -   #4
    Anyone?


  5. Internet, Programming and Graphics   -   #5
    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.
    Code:
    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?

  6. Internet, Programming and Graphics   -   #6
    Quote Originally Posted by ilw
    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.
    Code:
    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

    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.


  7. Internet, Programming and Graphics   -   #7
    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.

  8. Internet, Programming and Graphics   -   #8
    Im going for the bubble sort method. So far i have

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •