Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Problems in Printing combinations of numbers Posted by Ronnie_19 on 12 Jan 2007 at 5:00 PM
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
Report
Re: Problems in Printing combinations of numbers Posted by zibadian on 13 Jan 2007 at 1:30 AM
: 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:
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;

If IntToStr() doesn't exist, then you can write your own using the Str() procedure.
Report
Re: Problems in Printing combinations of numbers Posted by Ronnie_19 on 13 Jan 2007 at 1:40 AM

: You should use a recursive algorithm. Here's an untested example:
:
: 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
: 

: If IntToStr() doesn't exist, then you can write your own using the Str() procedure.
:

Report
Re: Problems in Printing combinations of numbers Posted by zibadian on 13 Jan 2007 at 2:05 AM
:
: : You should use a recursive algorithm. Here's an untested example:
: :
: : 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
: : 

: : 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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.