From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=0.2 required=3.0 tests=BAYES_05,FREEMAIL_FROM, FROM_STARTS_WITH_NUMS autolearn=no autolearn_force=no version=3.4.5-pre1 X-Received: by 2002:a37:a858:: with SMTP id r85mr8407967qke.3.1612017629020; Sat, 30 Jan 2021 06:40:29 -0800 (PST) X-Received: by 2002:a25:443:: with SMTP id 64mr1881021ybe.363.1612017628775; Sat, 30 Jan 2021 06:40:28 -0800 (PST) Path: eternal-september.org!reader02.eternal-september.org!news.swapon.de!news.in-chemnitz.de!3.eu.feeder.erje.net!feeder.erje.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sat, 30 Jan 2021 06:40:28 -0800 (PST) Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=85.242.255.219; posting-account=rhqvKAoAAABpikMmPHJSZh4400BboHwT NNTP-Posting-Host: 85.242.255.219 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Pascal triangle algorithm ("homework"). iterative one. the direct one was simple. From: Mehdi Saada <00120260a@gmail.com> Injection-Date: Sat, 30 Jan 2021 14:40:29 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader02.eternal-september.org comp.lang.ada:61243 List-Id: 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