AboutAlex Barry Expertise I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, c/c++, python, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.
Experience Hobby programming since 2000
I no longer belong to any community programming groups, but do have knowledge of *basic dialect and C/C++
Expert: Alex Barry Date: 2/28/2008 Subject: permutations
Question Hello.
I am not sure how to best do permutations.
I have 5 functions (a - e). Every function does something unique with an integer (for example multiply, divide, add etc.). I want to generate all possible sequences of the functions so that the functions could act on a given integer in different order. This could easily be done with 5 "for loops" but i don't want to use "for loops" (because next I will have 50 functions). What is the best way to create all these combinations of sequences without "for loops".
To be more clear, I am going to give an example with two functions: 1)A will add 1 to an integer, 2)B will subtract 1 from an integer.
The program should make all combinations and then do the math. Lets's say our integer is 5, then
combination 1: -- result 1: 5+0-0=5
combination 2: A- result 2: 5+1-0=6
combination 3: -B result 3: 5+0-1=4
combination 4: AB result 4: 5+1-1=5
How to do this combinatorics withour "for loops" what would a better approach.
Most thankfully, Andres
Answer Well, the only way QBasic could do it easily is with FOR/NEXT loops, which could be quite painful with 50 functions.
My suggestion would be to use FreeBASIC, if possible, where you can make variables represent functions, so it could work like this:
declare function add( byval num as integer, byval increm as integer ) as integer
declare function div( byval num as integer, byval divisor as integer ) as integer
dim funcs(1 to 2) as function( byval a as integer, byval b as integer ) as integer
funcs(1) = procptr( add )
funcs(2) = procptr( div )
dim number as integer
number = 5
number = funcs(1)( number, 1 ) ' add function, adding 1
print number ' Prints 6
number = funcs(2)( number, 3 ) ' div function, dividing by 3
print number ' Prints 2
it would still use FOR/NEXT, but it will be a dramatic amount less.
Unfortunately, QBasic isn't capable of the above code. If you can figure out a way to not use functions, QBasic might be able to do it. You could probably write an expression evaluator, a function that takes a string of math and evaluates it. it would work something like this:
dim result as integer
result = EVAL( "5+1/2" )
print result ' prints 3
EVAL functions are a little tricky, but if you follow BEDMAS rules, you should be fine. With that, you could do something like this:
then loop through those, creating various strings with your math.
I hope that does more help than confusion. If you need more help, do ask - I didn't really answer this question, but I hope that gives you some suggestions