From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_MSGID, SUBJ_ALL_CAPS autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f86c18fa05448c83,start X-Google-Attributes: gid103376,public From: Robert Byrne Subject: RECURSION PROBLEM Date: 1997/10/28 Message-ID: <34558FD4.2781@db.erau.edu>#1/1 X-Deja-AN: 286843118 Organization: Embry-Riddle Aeronautical University Newsgroups: comp.lang.ada Date: 1997-10-28T00:00:00+00:00 List-Id: I need to use recursion to display all pairs (combinations) of characters from a set of characters read from a file given on the command line. This is easy without recursion; the below program works correctly. I am thinking that the easiest way to modify this program would be to change the inner for loop in the procedure DISPLAY_COMBINATIONS to a recursive procedure. Does anyone have any idea how to go about doing this? I would appreciate any help. Thanks. byrner@db.erau.edu ------------------------------------------------------------------------ with ADA.TEXT_IO, ADA.COMMAND_LINE; use ADA.TEXT_IO; procedure PROG12 is INPUT_STRING: FILE_TYPE; --file to read from LENGTH: NATURAL:= 0; --length of string containing all characters TEMP_LENGTH: NATURAL:= 0; --length of string for each line LETTER: STRING(1..80); --string containing all characters TEMP_LETTER: STRING(1..80); --string for each line N: INTEGER:= 1; --counter for # of lines read procedure DISPLAY_COMBINATIONS(LETTER: in STRING; LENGTH: in NATURAL) is begin --DISPLAY_COMBINATIONS for I in 1..(LENGTH-1) loop for J in (I+1)..LENGTH loop PUT("("); PUT(LETTER(I)); PUT(", "); PUT(LETTER(J)); PUT(")"); NEW_LINE; end loop; end loop; end DISPLAY_COMBINATIONS; begin --PROG12 --open file to read from OPEN(INPUT_STRING, IN_FILE, ADA.COMMAND_LINE.ARGUMENT(1)); --input string from file INPUT_STRING while not END_OF_FILE(INPUT_STRING) loop --check for end of file GET_LINE(INPUT_STRING, TEMP_LETTER, TEMP_LENGTH); if TEMP_LENGTH /= 0 then --check for characters in line LENGTH:= LENGTH + TEMP_LENGTH; if N = 1 then LETTER(1..LENGTH):= TEMP_LETTER(1..LENGTH); else LETTER(1..LENGTH):= LETTER(1..LENGTH-TEMP_LENGTH) & TEMP_LETTER(1..TEMP_LENGTH); end if; N:= N+1; end if; end loop; --close input file CLOSE(INPUT_STRING); --determine and display all combinations of 2 characters NEW_LINE; DISPLAY_COMBINATIONS(LETTER, LENGTH); end PROG12; ------------------------------------------------------------------------