comp.lang.ada
 help / color / mirror / Atom feed
From: Brad Moore <bmoore.ada@gmail.com>
Subject: Re: Tally
Date: Tue, 14 Jan 2020 19:40:50 -0800 (PST)
Date: 2020-01-14T19:40:50-08:00	[thread overview]
Message-ID: <b806369f-e712-43e5-b6de-c80fb97dde96@googlegroups.com> (raw)
In-Reply-To: <qvlajt$95j$1@dont-email.me>

On Tuesday, January 14, 2020 at 2:08:16 PM UTC-7, Jeffrey R. Carter wrote:

> A lot depends on what restraints the problem domain puts on the input values. If 
> you can define something like
> 
> type Input_Number is range 1 .. 10;
> 
> then you can do something straightforward like
> 
> type Count_Map is array (Input_Number) of Natural;
> 
> Count  : Count_Map := (others => 0);
> Number : Input_Number;
> ...
> loop
>     exit when No_More_Numbers;
> 
>     Number := Next_Number;
>     Count (Number) := Count (Number) + 1;
> end loop;
> 
> If you can't limit the input numbers to a sufficiently small range that an 
> object like Count can be declared, then you'll need to use a map, as Holsti 
> suggested, which is only a little more complicated.
> 

Just to clarify, I'd say using a Map is more complex (quite a bit actually) in terms of what is happening semantically when the program executes, but I think it is worth noting that there is not necessarily much difference in complexity in terms of what the user has to write. As Jeff notes, using a map does has the advantage of flexibility, for example in handling sparsely populated data where there is a large range of input values.

e.g.

with Ada.Containers.Ordered_Maps; use Ada;

procedure Main
is
   package Count_Maps is new
     Containers.Ordered_Maps (Key_Type     => Natural,
                              Element_Type => Natural);
   
   type Input_Data is array (Natural range <>) of Natural;
   
   Input : Input_Data := (2, 3, 8, 2, 2, 2, 7, 2, 3, 4, 8);
   
   Counts : Count_Maps.Map;
      
begin
   for Number of Input loop
      Counts (Number) := Counts (Number) + 1;
   end loop;
end Main;

Brad

  reply	other threads:[~2020-01-15  3:40 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-14 15:27 Tally Gilbert Gosseyn
2020-01-14 16:22 ` Tally Niklas Holsti
2020-01-14 17:28   ` Tally Simon Wright
2020-01-15 11:52     ` Tally Simon Wright
2020-01-14 21:08 ` Tally Jeffrey R. Carter
2020-01-15  3:40   ` Brad Moore [this message]
2020-01-15  9:03     ` Tally Simon Wright
2020-01-15 23:09       ` Tally Jere
2020-01-16 11:34         ` Tally Simon Wright
2020-01-16 15:35           ` Tally Brad Moore
2020-01-16 20:20             ` Tally Randy Brukardt
2020-01-16 22:03               ` Tally Jeffrey R. Carter
2020-01-16 22:00             ` Tally Jeffrey R. Carter
2020-01-16 22:25               ` Tally Simon Wright
2020-01-17  2:51                 ` Tally Brad Moore
2020-01-17  3:08       ` Tally Brad Moore
replies disabled

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