Problems in Printing combinations of numbers - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Problems in Printing combinations of numbers

Ronnie_19Ronnie_19 Posts: 4Member
i am working a program with pascal. This program have to write all the possibility combinations which one can get from (1,2,3,4,5,6,7,8)
Note: These are not stored in an array

Each combination must be made up with all the 8 numbers and repetitions of numbers are not allowed

Comments

  • zibadianzibadian Posts: 6,349Member
    : i am working a program with pascal. This program have to write all the possibility combinations which one can get from (1,2,3,4,5,6,7,8)
    : Note: These are not stored in an array
    :
    : Each combination must be made up with all the 8 numbers and repetitions of numbers are not allowed
    :
    You should use a recursive algorithm. Here's an untested example:
    [code]
    procedure OutputCombinations(Out: string);
    begin
    if Length(Out) = 8 then
    writeln(Out)
    else begin
    for i := 1 to 8 do
    begin
    if Pos(IntToStr(i), Out) = 0 then
    Out := Out + IntToStr(i);
    OutputCombinations(Out); // Add next number
    end;
    end;
    [/code]
    If IntToStr() doesn't exist, then you can write your own using the Str() procedure.
  • Ronnie_19Ronnie_19 Posts: 4Member

    : You should use a recursive algorithm. Here's an untested example:
    : [code]
    : procedure OutputCombinations(Out: string);
    : begin
    : if Length(Out) = 8 then
    : writeln(Out)
    : else begin
    : for i := 1 to 8 do
    : begin
    : if Pos(IntToStr(i), Out) = 0 then
    : Out := Out + IntToStr(i);
    : OutputCombinations(Out); // Add next number
    : end;
    : end;




    Yes But I have to be restricted in using eight loops in order to form all possible numbers
    : [/code]
    : If IntToStr() doesn't exist, then you can write your own using the Str() procedure.
    :

  • zibadianzibadian Posts: 6,349Member
    :
    : : You should use a recursive algorithm. Here's an untested example:
    : : [code]
    : : procedure OutputCombinations(Out: string);
    : : begin
    : : if Length(Out) = 8 then
    : : writeln(Out)
    : : else begin
    : : for i := 1 to 8 do
    : : begin
    : : if Pos(IntToStr(i), Out) = 0 then
    : : Out := Out + IntToStr(i);
    : : OutputCombinations(Out); // Add next number
    : : end;
    : : end;
    :
    :
    :
    :
    : Yes But I have to be restricted in using eight loops in order to form all possible numbers
    : : [/code]
    : : If IntToStr() doesn't exist, then you can write your own using the Str() procedure.
    : :
    :
    :
    This produces 8 loops, since the function is only called 8 times: once for each number in the sequence.
Sign In or Register to comment.