Array and creation of the elements of the array 
Author Message
 Array and creation of the elements of the array

Quote:

> ... I want an array of 2 complexes. I want 1.4+8.9i for

>    local
>       a:ARRAY[COMPLEX]
>    do
>       !!a.make(0,1)  -- Create the array from index 0 to 1
>       !!(a.item(0)).make(1.4, 8.9)  -- I have a compilation error
>       !!(a.item(1)).make(6.5, 9.8)  -- I have a compilation error

It is only possible to apply a creation feature to a
named entity, not to an expression.

In your example, the expression "a.item(0)" returns a void reference, which
cannot be the target of a creation.

Here's one way to solve your problem:

   local
      c: COMPLEX
      a: ARRAY[COMPLEX]
   do
      !!a.make(0, 1)
      !!c.make(1.4, 8.9)
      a.put(c, 0)
      !!c.make(6.5, 9.8)
      a.put(c, 1)

Another way is to write a function that creates and returns a new
instance of COMPLEX. This can be used on the right-hand-side, within
a manifest array:

   new_complex(r, i: REAL): COMPLEX is
      do
         !!result.make(r, i)
      end

   build_complex_array is
      local
         a: ARRAY[COMPLEX]
      do
         a := << new_complex(1.4, 8.9), new_complex(6.5, 9.8) >>
         ...

[Aside: Unlike many other languages, Eiffel does not provide a built-in way
to create new objects on the right-hand-side of an assignment instruction.
I have converted quite a lot of delphi code to Eiffel, and have found three
main reasons why the Eiffel version uses more lines. The lack of creation on
the right-hand-side is one of those, and means that one Delphi statement can
translate to up to a dozen Eiffel lines plus an extra local attribute or two.
Eiffel loops also take up more lines, and the remaining growth in the
size of the source files is most welcome, being due to assertions.]

Regards,
Roger
--
--
-- Roger Browne, 6 Bambers Walk, Wesham, PR4 3DG, UK | Ph 01772-687525
-- Everything Eiffel: http://www.*-*-*.com/ | +44-1772-687525



Thu, 04 Nov 1999 03:00:00 GMT  
 Array and creation of the elements of the array

Quote:

> Hello,

> I have a COMPLEX class with a creator called "make" that need two
> arguments (The first is the real part of the complex and the
> second is the imaginary part).

> So to define a complex like 1.2+3.4i referenced by c, I write:

>    local
>       c:COMPLEX
>    do
>       !!c.make(1.2, 3.4)

> Now, I want an array of 2 complexes. I want 1.4+8.9i for

>    local
>       a:ARRAY[COMPLEX]
>    do
>       !!a.make(0,1)  -- Create the array from index 0 to 1
>       !!(a.item(0)).make(1.4, 8.9)  -- I have a compilation error
>       !!(a.item(1)).make(6.5, 9.8)  -- I have a compilation error

> Do you know how to solve the problem. Thank you very much
> for your help.

   The basic technique is to create your COMPLEX objects and put them
into the array:

    local
       array: ARRAY [ COMPLEX]
       complex: COMPLEX
    do
       !!array.make ( 0, 1)  -- Create the array from index 0 to 1
       !!complex.make ( 1.4, 8.9)
       array.put ( complex, 0)
       !!complex.make ( 6.5, 9.8)
       array.put ( c, 1)

 Or:

    local
       array: ARRAY [ COMPLEX]
       complex1, complex2: COMPLEX
    do
       !!complex1.make ( 1.4, 8.9)
       !!complex2.make ( 6.5, 9.8)
       !!array.make_from_array ( << complex1, complex2>>)

If there are other special functions of complex arrays*, it might be
worth creating a class COMPLEX_ARRAY and adding the creation routine:

    make_from_set ( initial_values: ARRAY [ ARRAY [ REAL]])

And constructing your array:

    !!array.make_from_set ( << <<1.4, 8.9>>, <<6.5, 9.8>> >>)

*If you find that your class contains utility functions especially for
manipulating arrays of complex's, then this is a hint that COMPLEX_ARRAY
is a viable and possibly re-usable abstraction.  If those features turn
out to apply to non-complex values as well, maybe a more generic class
could be created, taking a type parameter like ARRAY does.

- Roy
--
 Roy Phillips, Information Architect
 The Technical Resource Connection, Inc.
 (Subsidiary of Perot Systems Corporation)
 ------------------------------------------------------
  12320 Racetrack Road    | Tel:  (813)891-6084
  Tampa                   |       1-800-TRC-2992
  Florida 33626           | Fax:  (813)891-6138

 ------------------------------------------------------



Fri, 05 Nov 1999 03:00:00 GMT  
 Array and creation of the elements of the array

Quote:
> Hello,

> I have a COMPLEX class with a creator called "make" that need two
> arguments (The first is the real part of the complex and the
> second is the imaginary part).

> So to define a complex like 1.2+3.4i referenced by c, I write:

>    local
>       c:COMPLEX
>    do
>       !!c.make(1.2, 3.4)

> Now, I want an array of 2 complexes. I want 1.4+8.9i for

>    local
>       a:ARRAY[COMPLEX]
>    do
>       !!a.make(0,1)  -- Create the array from index 0 to 1
>       !!(a.item(0)).make(1.4, 8.9)  -- I have a compilation error
>       !!(a.item(1)).make(6.5, 9.8)  -- I have a compilation error

    local
        a: ARRAY [COMPLEX]
        c: COMPLEX
    do
        !!a.make (0, 1)
        !!c.make (1.4, 8.9)
        a.put (c, 0)
        !!c.make (6.5, 9.8)
        a.put (c, 1)
        ...

Will work.

Nick



Fri, 05 Nov 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. creation of array elements and write traces on an array

2. Shifting array element & regex on array element

3. Arrays: Build array in multiple for loops or replace array elements

4. Access Array Elements by Arrays Reference

5. for every element of array find bounds in another array

6. Adjustable array dimensions specified via array element?

7. selecting array elements with a logical array

8. (typep (make-array 10 :element-type 'bit) '(array bit (10)))

9. Do array element traces affect the whole array?

10. An array as an array element?

11. overhead of tracing arrays vs. array elements

12. HOWTO: reference elements of an array if array name is in a variable

 

 
Powered by phpBB® Forum Software