Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assistance required in setting up league

Status
Not open for further replies.

al255

Programmer
Jun 18, 2007
1
GB
Hi,

I need assistance with trying to set up a little soccer league.

I have some code which allows me to sort the higher numbers from the lower numbers. What I really want is the numbers/points to move along with their chosen team and in ascending order (I have no idea how to do this). If Team B (see below) gains more points than Team A, then Team B should move to position 1.
For example;
1. Team A 13
2. Team B 9
3. Team C 6

How can I do this ??

Any help is appreciated.

Thanks,
Al255
 

"How can I do this ??"

This will require the use of at least a one two-dimensional array.

ex:

DIM SocTeam(3, 1)

Look up in the help files for the DIM and possibly the SWAP statements. There should also be some examples that you can follow from.
--MiggyD

Never be afraid to try something new. Remember that amateurs built the Ark. Professionals built the Titanic.
 
al255,

For now, we'll pretend there are 3 Teams, named team$(1) thru team$(3).

The scores are held in teamscore(1) thru teamscore(3).

We'll also use team999$(1) thru team999$(3) to temporarily hold the info for sorting purposes.

For now, this is the intial array:

team$(1) ="Team A" : teamscore(1) = 6
team$(2) ="Team B" : teamscore(2) = 4
team$(3) ="Team C" : teamscore(3) = 8

Write this simple loop:

For g = 1 to 3
team$ = MID$(STR$(teamscore(g)), 2)
team$ = STRING$(3 - LEN(team$), 48) + team$
team999$(g) = team$ + team$(g)
PRINT team999$(g)
next

You'll see this:
006Team A
004Team B
008Team C

As you can see, now the score becomes the governing sort feature of the array.

Now, sort as you have been. For this example, use the team999$() array.

After the sort, use a routine such as this to print the info:

FOR g = 1 TO 3
PRINT MID$(team999$(g), 4); " Standings> "; val(LEFT$(team999$(g), 3))
NEXT

You'll now see this
Team C Standings > 8
Team A Standings > 6
Team B Standings > 4

Of course, you can set this up anyway you wish. PRINT USING is recommended.

If you ever wanted to add the Ties and Losses to provide accurate standings, just use the example above. It would look like this:

008002003Team C


Hope this helps.

Dr. Jon





 
This does not look like sorting at all :) I have a FAQ on quicksorting: faq314-336

The basic idea is that you want to re-order the elements to put the largest scores at the top. There are a number of ways to achieve this. If you don't want to set up a "complex" sorting routine like the quicksort, you can write a so-called "bubble" sort fairly easily. Be aware that the bubblesort is very inefficient, and this becomes apparent for larger numbers of elements (i.e., you would not want to run it on an array 5,000 elements long; it would probably take several minutes). Here is what a bubble sort might look like (look for the 'SUB bubbleSort' near the end):
[tt]
TYPE teamType
teamName AS STRING * 10
teamScore AS INTEGER
END TYPE

DIM
team(1 TO 3) AS teamType

setupTeams team()

PRINT "Before sorting:"
showStats team()

bubbleSort team()

PRINT "After sorting:"
showStats team()

END

SUB
setupTeams(team() AS teamType)
FOR i% = LBOUND(team) TO UBOUND(team)
team(i%).teamName = "Team " + CHR$(i% - LBOUND(team) + 65)
team(i%).teamScore = INT(RND * 30)
NEXT i%
END SUB

SUB
showStats(team() AS teamType)
FOR i% = LBOUND(team) TO UBOUND(team)
PRINT team(i%).teamName; ":"; team(i%).teamScore; "points"
NEXT i%
END SUB

SUB
bubbleSort(team() AS teamType)
FOR i% = LBOUND(team) TO UBOUND(team) - 1
FOR j% = LBOUND(team) TO UBOUND(team) - (i% - LBOUND(team) + 1)
IF team(j%).score > team(j% + 1).score) THEN SWAP team(j%), team(j% + 1)
NEXT j%
NEXT i%
END SUB
[/tt]

To see how this works, you have to look at what the inside [tt]j%[/tt] loop does. Each time through the loop, it starts at the beginning and goes up to an index value one less than the previous time. On each element, it compares it with the next one, and swaps them if they are out of order. With a little bit of thought, it can be seen how this will move the highest, followed by the next-highest, etc., up to the "top" of the list, while slowly "bubbling" the lower values down the list. Here is an example:

Starting with the following list:

3
7
2
4
5

..the comparisons & swaps performed by the bubblesort are as follows (comparisons are blue, comparisons w/ swaps are red):

First 'inner' loop:
3
7

2
4
5

3
7 2
2 7

4
5

3
2
7 4
4 7

5

3
2
4
7 5
5 7


Second 'inner' loop:
3 2
2 3

4
5
7

2
3
4

5
7

2
3
4
5

7

Third 'inner' loop:
2
3

4
5
7

2
3
4

5
7

Fourth and final 'inner' loop:
2
3

4
5
7

You can see that at the end of the first inner loop, the highest value has been moved to the end of the list. Though nothing is actually moved, you can also see that the last comparison of each of the other inner loops ends with the largest value up to that point in the correct position. More importantly, though, you can see that a lot of unnecessary comparisons are made by the bubblesort. This is part of the reason why it is so inefficient. For larger arrays, the quicksort should be used, and it is described, as mentioned earlier, in faq314-336
 
LOGICLRD,

I assume this line was directed at me:

This does not look like sorting at all. I have a FAQ on quicksorting.

You missed the line where I said:

Now, sort as you have been. For this example, use the team999$() array.

I didn't feel the need to share another sort routine since the poster had mentioned he already had one.

Your example is fine but it relies upon programming that may indeed be too complex for the end user. I am tryng to point out that anyone may use the computer's ability to manipulate String$s to their advantage. Instead of nested loops, in many instances all pertinent info may be placed into one array, not two, three or whatever.

So, using my example, if Team C has more wins than Team A, the user need simply attach 008 to the name. When sorted with a simple Swap, the task is completed instantly because only one sort is required. And the user need not be concerned about complex looping procedures.

I'm certain the soccer league has few teams, so in effect, he really could store all of the league info into one String$ instead of an array but that shall wait until another time.

The tried and true formulas are wonderful but when there are easier and to the point methods, they should be shared. I think that if IL255 simply types in my example and uses his own sort, he'll find this to be a simple and understandble way of doing this. And exactly what he requested. All tolled, his entire objective can be completed in under 10 lines of code.

Jon


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top