From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Q: Trouble creating array and discriminated type Date: Sat, 21 Jan 2017 07:23:23 +0000 Organization: A noiseless patient Spider Message-ID: References: <83409c51-59d3-4205-9eeb-5467de09f069@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="f71855879c13dd065018d52bf52ac20c"; logging-data="3152"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Nnmkfod8/YauUWM3zLm94LvKEjxWWctM=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (darwin) Cancel-Lock: sha1:5/4/HO1jgNbZ6RVW51ovsbkN0jY= sha1:d9AS1aE3rkaresnJ0nyGVXmrnmk= Xref: news.eternal-september.org comp.lang.ada:33109 Date: 2017-01-21T07:23:23+00:00 List-Id: b.mcguinness747@gmail.com writes: > First, gnatmake complains about two constant arrays I am trying to > create. I have: > > subtype Int2 is Integer range -32768..32767; > type Real is digits 16; > > ... > > a_napl_t : constant array (0..677,0..13) of Int2 := ( > ( 0, 0, 0, 0, 0, 0, 0, 8,-16, 4, 5, 0, 0, 0), > ( 0, 0, 0, 0, 0, 0, 0, -8, 16, -4, -5, 0, 0, 2), > > ... > )); Addressing the first problem only (for the moment) -- It's more normal Ada to let the compiler work out the size of the array from the size of the data, something like this: =============OX=============== with Ada.Text_IO; use Ada.Text_IO; procedure B_M_1 is subtype Int2 is Integer range -32768 .. 32767; type Real is digits 6; -- shorter string representation, see at end -- Declare an unconstrained array type, indexed from 0 (you -- probably don't want to use Integer here, because initialization -- with an aggregate will result in an object with indices running -- from Integer'First, -2**31, leading to confusion) type Napl_Data is array (Natural range <>, Natural range <>) of Int2; A_Napl_T : constant Napl_Data := ( ( 0, 0, 0, 0, 0, 0, 0, 8,-16, 4, 5, 0, 0, 0), ( 0, 0, 0, 0, 0, 0, 0, -8, 16, -4, -5, 0, 0, 2) ); -- This time, make the indices run from 1 (many Ada programmers -- think this more natural :-) type Cpl_Data is array (Positive range <>, Positive range <>) of Real; A_Cpl_T : constant Cpl_Data := ( ( 1440.0, 0.0, 0.0, 0.0), ( 56.0, -117.0, -42.0, -40.0), ( 125.0, -43.0, 0.0, -54.0) ); begin -- Print out the ranges (NB 'Img is a GNAT extension, not standard Ada) Put_Line ("A_Napl_T range: " & A_Napl_T'First (1)'Img & " .. " & A_Napl_T'Last (1)'Img & ", " & A_Napl_T'First (2)'Img & " .. " & A_Napl_T'Last (2)'Img); Put_Line ("A_Cpl_T range: " & A_Cpl_T'First (1)'Img & " .. " & A_Cpl_T'Last (1)'Img & ", " & A_Cpl_T'First (2)'Img & " .. " & A_Cpl_T'Last (2)'Img); -- Print out the values (of the shorter array!) for J in A_Cpl_T'Range (1) loop for K in A_Cpl_T'Range (2) loop Put (A_Cpl_T (J, K)'Img & " "); end loop; New_Line; end loop; end B_M_1; =============OX=============== which gives $ ./b_m_1 A_Napl_T range: 0 .. 1, 0 .. 13 A_Cpl_T range: 1 .. 3, 1 .. 4 1.44000E+03 0.00000E+00 0.00000E+00 0.00000E+00 5.60000E+01 -1.17000E+02 -4.20000E+01 -4.00000E+01 1.25000E+02 -4.30000E+01 0.00000E+00 -5.40000E+01