# How to allocate an array according to the type of a computation to follow?

**URL:** <https://discourse.julialang.org/t/how-to-allocate-an-array-according-to-the-type-of-a-computation-to-follow/55040>\
**Category:** New to Julia\
**Created:** [February 11, 2021, 3:01am UTC](https://discourse.julialang.org/t/how-to-allocate-an-array-according-to-the-type-of-a-computation-to-follow/55040 "2021-02-11T03:01:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [February 11, 2021, 3:01am UTC](https://discourse.julialang.org/t/how-to-allocate-an-array-according-to-the-type-of-a-computation-to-follow/55040/1 "2021-02-11T03:01:21Z")

</div>

I tried the following example

```nohighlight
function test(a,b, obs)
    T = typeof(a * b)
    Z = Array{T}(obs)
    for i in 1:obs
        Z[i] = a * b / i
    end
    return Z
end
test(5.0,2.0, 3)
test(Dual(5.0, 1,0), Dual(2.0, 0,1), 3)

```

but it errors with `ERROR: LoadError: MethodError: no method matching (Array{Float64, N} where N)(::Int64)`. How may I correct this script?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 11, 2021, 3:02am UTC](https://discourse.julialang.org/t/how-to-allocate-an-array-according-to-the-type-of-a-computation-to-follow/55040/2 "2021-02-11T03:02:21Z")

</div>

> [@askvorts](#):
>
> How may I correct this script?

Use `T = typeof(a * b / 1)` and `Z = Array{T}(undef, obs)`

---

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [February 11, 2021, 3:06am UTC](https://discourse.julialang.org/t/how-to-allocate-an-array-according-to-the-type-of-a-computation-to-follow/55040/3 "2021-02-11T03:06:54Z")

</div>

Worked like a charm. Thank you very much!

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [February 11, 2021, 3:07am UTC](https://discourse.julialang.org/t/how-to-allocate-an-array-according-to-the-type-of-a-computation-to-follow/55040/4 "2021-02-11T03:07:27Z")

</div>

You can also just let Julia create the array for you with the right type:

```julia
function test(a,b, obs)
    map(1:obs) do i
        a * b / i
    end
end

```
