comp.lang.ada
 help / color / mirror / Atom feed
* permutation of strings
@ 2014-10-07  5:23 Stribor40
  2014-10-07  5:38 ` mockturtle
  0 siblings, 1 reply; 3+ messages in thread
From: Stribor40 @ 2014-10-07  5:23 UTC (permalink / raw)


I have created this code using some online tutorials and some examples that i tried to learn... i havent been able to find whats wrong with my code....i am guessing what my variable j is somehow getting reset during the recursive calls...
i have been trying to retrace recursive calls by writing each call on the on the paper but i get lost trying to track it down......

my code spits out numbers...123 123 123 123 123 123 instead of 123 132 213 231 321 312 so i was hoping if someone can take a look and suggest how to fix this....

i pasted the code here it is ready to be compiled and there are no errors when it is compiled....http://pastebin.com/rDF46RbR



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: permutation of strings
  2014-10-07  5:23 permutation of strings Stribor40
@ 2014-10-07  5:38 ` mockturtle
  2014-10-07 11:22   ` Brian Drummond
  0 siblings, 1 reply; 3+ messages in thread
From: mockturtle @ 2014-10-07  5:38 UTC (permalink / raw)


On Tuesday, October 7, 2014 7:23:53 AM UTC+2, Stribor40 wrote:
> I have created this code using some online tutorials and some examples that i tried to learn... i havent been able to find whats wrong with my code....i am guessing what my variable j is somehow getting reset during the recursive calls...
> 
> i have been trying to retrace recursive calls by writing each call on the on the paper but i get lost trying to track it down......
> 
> my code spits out numbers...123 123 123 123 123 123 instead of 123 132 213 231 321 312 so i was hoping if someone can take a look and suggest how to fix this....
> 
> i pasted the code here it is ready to be compiled and there are no errors when it is compiled....http://pastebin.com/rDF46RbR

At the moment I do not have a compiler at hand (I'm having breakfast :-), but I see at least one strange thing in your code: in the loop "for Index ...." I would expect calls to "change(s, i, Index)" rather than "change(s, i, j)".  With the latter there is no difference between different iterations and this could give rise to your error.

By the way, there is no need to have j declared "in out" in Change, since j is not changed. Moreover, I suspect that with the "in out" specification the compiler will complain, since you cannot change a loop index.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: permutation of strings
  2014-10-07  5:38 ` mockturtle
@ 2014-10-07 11:22   ` Brian Drummond
  0 siblings, 0 replies; 3+ messages in thread
From: Brian Drummond @ 2014-10-07 11:22 UTC (permalink / raw)


On Mon, 06 Oct 2014 22:38:25 -0700, mockturtle wrote:

> On Tuesday, October 7, 2014 7:23:53 AM UTC+2, Stribor40 wrote:
>> I have created this code using some online tutorials and some examples
>> that i tried to learn... i havent been able to find whats wrong with my
>> code....i am guessing what my variable j is somehow getting reset
>> during the recursive calls...
>> 
>> i have been trying to retrace recursive calls by writing each call on
>> the on the paper but i get lost trying to track it down......
>> 
>> my code spits out numbers...123 123 123 123 123 123 instead of 123 132
>> 213 231 321 312 so i was hoping if someone can take a look and suggest
>> how to fix this....
>> 
>> i pasted the code here it is ready to be compiled and there are no
>> errors when it is compiled....http://pastebin.com/rDF46RbR
> 
> At the moment I do not have a compiler at hand (I'm having breakfast
> :-), but I see at least one strange thing in your code: in the loop "for
> Index ...." I would expect calls to "change(s, i, Index)" rather than
> "change(s, i, j)".  With the latter there is no difference between
> different iterations and this could give rise to your error.

Nailed it by inspection!

> By the way, there is no need to have j declared "in out" in Change,
> since j is not changed. Moreover, I suspect that with the "in out"
> specification the compiler will complain, since you cannot change a loop
> index.

Having corrected the real bug, it did complain...

If I might add a couple more style changes:
1) Eliminate global variable "tmp" which should be local to procedure 
"change" 
2) Eliminate global variable "Start" and potential for off-by-1 bugs, by 
using the array attributes in the main call to Perms

Works as I would expect anyway.

- Brian
---------------------------------------------

with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;

procedure permutation is

  str : String := "123";

  procedure perms (s :  in out String; i : in Integer; n : in Integer);
  procedure change (s : in out String; i : in Integer; j : in Integer);

  procedure  perms (s : in out String; i : in Integer; n : in Integer) is
    j : Integer := i;
  begin
    if i = n then
      put(s);
      New_Line;
    else
      for Index in j..n loop
        change(s,i,index); 
        perms(s,i+1,n);
        change(s,i,index); 
      end loop;
    end if;
  end perms;
  
 
  procedure  change (s : in out String; i : in  Integer; j : in Integer) 
is
    tmp : Character;
  begin
    tmp  := s(i);
    s(i) := s(j);
    s(j) := tmp;
 end change;

begin
    perms(str,str'first,str'last);
end permutation;



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-10-07 11:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-07  5:23 permutation of strings Stribor40
2014-10-07  5:38 ` mockturtle
2014-10-07 11:22   ` Brian Drummond

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox