Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.