Re: How to solve for smallest and largest int?

From: CBFalconer (cbfalconer_at_yahoo.com)
Date: 03/27/04


Date: Sat, 27 Mar 2004 20:37:24 GMT

James Rogers wrote:
>
... snip ...
>
> A simple example is a function that totals all the elements in an
> array: Given the following data type definition:
>
> type My_Array is array(Natural range <>) of Integer;
>
> The following function will correctly total all the elements in
> the array passed in to the formal parameter "Item", no matter how
> many elements the array contains.
>
> function total(Item : My_Array) return Integer is
> Sum : Integer := 0;
> begin
> for i in Item'Range loop
> Sum := Sum + Item(i);
> end loop;
> return Sum;
> end total;
>
> The 'for' loop syntax iterates exactly through the range of index
> values for the array. There is no opportunity for an array bound
> violation. The compiler will omit bounds checking within the
> 'for' loop.
... snip ...

For contrast the average C (or assembly) programmer must consider
many mundane details, as in:

  typedef struct My_int_Array {
     size_t size;
     int *data;
  } My_int_Array;

and consider the task of assigning memory to *data after size has
been determined. Now the summing operation can be coded:

   int Total(const My_int_Array *array)
   {
      int val;
      size_t i;

      for (val = 0, i = 0; i < array->size; i++)
         val += (array->data)[i];
      return val;
   }

The totalling code is probably clearer, but the details of
initializing and filling that array are going to be much more
complex. Notice the C code has not handled integer overflow in
any way. That could be added, and now the C would no longer be
very clear. There would be a major problem about what to do on an
overflow.

... snip ...
>
> Many stupid programming behaviors are *not* the result of
> programmers trying to do something clever. Many are the result
> of simple lack of attention to detail, or laziness. Ada provides
> an automated attention to details and an effort to compensate
> for laziness as its default behavior. Clever programmers who are
> paying attention to the details and are not lazy will, when
> appropriate, turn off the default run time checking and not be
> inhibited by the language.

However the programmer who has never used the dangerous languages,
such as C and assembly, is not able to appreciate the possibility
and benefit of that default checking.

-- 
Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses.  - James Rhodes.


Relevant Pages