' N. V. Fitton ' Math 577 ' Project 1 ' January 11, 1996 ' This program takes a set from the user ' and then generates subsets of random size ' until the user has had enough. ' The method used for generating a random subset ' of a set of N members is: ' generate a random whole number X in the range [0,N] ' randomly choose X items from the set ' Input is checked to see that no element occurs more than once. ' A set of indices is used to refer to the elements of the set, ' so that choosing X elements of the set is actually ' accomplished by choosing X indices. ' Duplicates are not allowed in this choice. ' ----- Initialization --------------------------------------------- ' ------------------------------------------------------------------ DIM s$(10) ' ------ the user's set ----- DIM i0%(10) ' ------ an index set for array s$ ----- CLS PRINT "" PRINT "" PRINT " This program will prompt you to enter a set of as many as " PRINT " ten items. It will then generate subsets of your set" PRINT " until you tell it to stop." PRINT "" RANDOMIZE TIMER ' ----- initialize index set ----- FOR i% = 1 TO 10 i0%(i%) = i% NEXT i% ' ----- Get a set from user ---------------------------------------- ' ------------------------------------------------------------------ PRINT " Enter the elements of your set, one to a line." PRINT " Hit return after each element." PRINT " When you have finished, hit return again." PRINT "" GOSUB getSet: IF s$(1) = "" THEN PRINT " Bye!!" GOTO theEnd ELSE PRINT " Here is your set: " GOSUB printSet: END IF ' ----- Create and print subsets ----------------------------------- ' ------------------------------------------------------------------ newSubset: PRINT "" PRINT " Here is a randomly generated subset of your set:" ' ----- get a random number (called X%) ' ----- in [0,N] X% = INT(RND * (N% + 1)) IF X% = 0 THEN ' ----- subset is empty ----- n9% = 0 ELSE ' ----- or ----- n9% = X% FOR i% = 1 TO X% ' ----- choose X% distinct indices in [1,N] nextIndex: i0%(i%) = INT(RND * N%) + 1 FOR j% = 1 TO (i% - 1) IF i0%(i%) = i0%(j%) THEN GOTO nextIndex: END IF NEXT j% NEXT i% END IF GOSUB printSet ' ----- print result ----- ' ----- offer another round ----- INPUT " Another (Y if yes)? ", y$ IF ((y$ = "y") OR (y$ = "Y")) THEN GOTO newSubset: END IF theEnd: END ' subroutine ---------------------------------------------------- getSet: ' ---------------------------------------------------- FOR i% = 1 TO 10 nextEl: INPUT " Next element? ", a$ IF a$ <> "" THEN FOR j% = 1 TO (i% - 1) ' ----- check input ----- IF a$ = s$(j%) THEN PRINT " No element can occur more than once!" GOTO nextEl: END IF NEXT j% s$(i%) = a$ ' ----- accept this input ----- N% = N% + 1 ELSE i% = 11 ' ----- end of input ----- END IF NEXT i% n9% = N% PRINT "" RETURN ' subroutine ---------------------------------------------------- printSet: ' ---------------------------------------------------- PRINT "" IF n9% = 0 THEN PRINT " { } " ELSE PRINT " { "; FOR k% = 1 TO (n9% - 1) PRINT s$(i0%(k%)); PRINT ", "; NEXT k% PRINT s$(i0%(n9%)); PRINT " }" END IF PRINT "" RETURN '-------N V Fitton-----nv.fitton@uc.edu-----University d'Cincinnati------- '------(----------)------------------------Math and Computer Science------