comp.lang.ada
 help / color / mirror / Atom feed
From: Mehdi Saada <00120260a@gmail.com>
Subject: Pascal triangle algorithm ("homework"). iterative one. the direct one was simple.
Date: Sat, 30 Jan 2021 06:40:28 -0800 (PST)	[thread overview]
Message-ID: <b5b2a4fe-cb96-4166-a99c-1527cc2849dcn@googlegroups.com> (raw)

Here's this, complete with put_line and comments...
Sorry for the length. I can't even follow anymore where's it going wrong and why. tried to. At least the "swap" routine does what it 's supposed to.
I hope you'll enlighten me.

with Ada.Text_IO;
use Ada.Text_IO;

procedure Pascal is
   type Tab_type is array(Natural range <>) of Natural;
   function Factorial (Number: Natural) return Positive is
      result: Positive := 1;
   begin
      for I in 1..Number loop
         result := Result * I;
      end loop;
      return result;
   end Factorial;
   function Give_Pascal_factoriel (N_rank: in Natural) return Tab_type is
      Result: Tab_type(0..N_rank);
   begin
      if N_rank+1 mod 2 = 0 then
         for I in 0..N_rank/2 loop
            Result(I) := Factorial(N_rank) / (Factorial(N_rank-I)*Factorial(I));
         end loop;
         Result(N_rank/2+1..N_rank) := Result(0..N_rank/2);
      else
         for I in 0..N_rank/2 loop
            Result(I) := Factorial(N_rank) / (Factorial(N_rank-I)*Factorial(I));
            Result(N_rank-I) := Result(I);
         end loop;
      end if;
      return Result;
   end Give_Pascal_factoriel;
   function Give_Pascal_iterative (N_rank: in Natural) return Tab_type is
      Temp1, Temp2: Tab_type(0..N_rank+2):= (1 => 1, others => 0);
      Index1, Index2: Natural;
      procedure Swap(Temp2, Temp1: in out Tab_type;Index2, Index1: in out Natural) is
         temp_vrai_index : Natural := index1;
         Temp_vrai: Tab_type := temp1;
      begin
         temp1(0..index2) := temp2(0..Index2);
         index1 := index2;
         temp2 := Temp_vrai;
         index2 := temp_vrai_index;
      end Swap;
   begin
      if N_rank = 0 then return Tab_type'(0=> 1);
      elsif N_rank = 1 then return (1,1);
      else
         temp1(0..2) := (0,1,1);
         index1 := 2;
         -- each instance of this loop creates a new line on array 2 from array1
         -- then arrays are swapped.
         for N in 1..N_rank loop
            -- each instance of the inner loop fill one slot of array2 up to N/2, and reciprocally on the other side of the array
            -- then in case the number of coefficients for the nth line is even
            -- the last one at place N/2+1 (considering the array starts at 0);

            for I in 1..N/2 loop
               Put_Line("I = " & I'Image);
               Put_Line("temp1(I) : " & temp1(I)'Image);
               Put_Line("temp1(I-1): " & temp1(I-1)'Image);
               temp2(I) := temp1(I-1) + temp1(I);
               Put_Line("temp2((I)'Image is now :" & temp2(I)'Image);
            end loop;
            Put_Line("temp2 :" );
            for A of temp2(1..N/2) loop
               Put_Line(A'Image);
            end loop;
            if N mod 2 = 0 then
               Put_Line("N = " & N'Image & " so number of coefficients is odd");
               temp2(N/2+1) := temp1(N/2) + temp1(N/2+1);
            end if;
            Index2 := index1 + 1;         
            Swap(Index2 => Index2, Index1 => Index1,
                 Temp2  => Temp2, Temp1  => Temp1);
          
            -- index1 and index2 serve only to swap the arrays,
            -- whose size is constant and put to the max they'll have to be.
         end loop;
      end if;
      return Temp2(1..N_rank+2);
   end Give_Pascal_iterative;
   tab_result : Tab_type(0..6);
begin
   tab_result := Give_Pascal_iterative(N_rank => 5);
   New_Line;
   
   Put_Line("attempt");
   for A  of tab_result loop
      Put_Line(A'Image);
   end loop;
end Pascal;

Listing: /bin/sh -c /home/mehdi/obj/pascal
temp2 :
I =  1
temp1(I) :  1
temp1(I-1):  0
temp2((I)'Image is now : 1
temp2 :
 1
N =  2 donc nombre de cefficients impairs
I =  1
temp1(I) :  1
temp1(I-1):  0
temp2((I)'Image is now : 1
temp2 :
 1
I =  1
temp1(I) :  1
temp1(I-1):  0
temp2((I)'Image is now : 1
I =  2
temp1(I) :  0
temp1(I-1):  1
temp2((I)'Image is now : 1
temp2 :
 1
 1
N =  4 donc nombre de cefficients impairs
I =  1
temp1(I) :  1
temp1(I-1):  0
temp2((I)'Image is now : 1
I =  2
temp1(I) :  1
temp1(I-1):  1
temp2((I)'Image is now : 2
temp2 :
 1
 2

essai
 1
 1
 0
 0
 0
 0
 0
[2021-01-30 14:34:20] process terminated successfully, elapsed time: 01.04s

             reply	other threads:[~2021-01-30 14:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-30 14:40 Mehdi Saada [this message]
2021-01-30 16:36 ` Pascal triangle algorithm ("homework"). iterative one. the direct one was simple DrPi
2021-01-30 16:55   ` Mehdi Saada
2021-01-30 23:42     ` Mehdi Saada
replies disabled

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