# I want my code to display all the five results after each iteration

**URL:** <https://discourse.julialang.org/t/i-want-my-code-to-display-all-the-five-results-after-each-iteration/79864>\
**Category:** New to Julia\
**Created:** [April 22, 2022, 7:44pm UTC](https://discourse.julialang.org/t/i-want-my-code-to-display-all-the-five-results-after-each-iteration/79864 "2022-04-22T19:44:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 22, 2022, 7:44pm UTC](https://discourse.julialang.org/t/i-want-my-code-to-display-all-the-five-results-after-each-iteration/79864/1 "2022-04-22T19:44:19Z")

</div>

I want my code to display all the five results after each iteration and also select the maximum among the 5 results and display it. When I run it I only get one output. All five results are not showing. I have an array of five elements each. I am writing a code to take an element and find the radius using the formula in my code at each of this values, I have 5 values in each array. Any help will be appreciated please. Attached is the code.

a=[0 2.5 -2.5 2 -2.5]  
b=[0 0.3 0.25 -2.5 -2.25]  
r=[4 8 9 8 8]  
(x,y)=(0,0)

function radius(x,y,a,b,r)  
n=Float64  
for (i,m) in enumerate(a)  
R=Float64

```
         push!(R, (((a[i]-x)^2 + (b[i]-y)^2)^0.5 + r[i] ) )

```

end  
push!(n, R)

return n

end

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [April 22, 2022, 8:03pm UTC](https://discourse.julialang.org/t/i-want-my-code-to-display-all-the-five-results-after-each-iteration/79864/2 "2022-04-22T20:03:27Z")

</div>

Please format your code by using 3 backticks (i.e. ```) on the line before your code and 3 on the line after it.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [April 22, 2022, 8:54pm UTC](https://discourse.julialang.org/t/i-want-my-code-to-display-all-the-five-results-after-each-iteration/79864/3 "2022-04-22T20:54:39Z")

</div>

The code that you posted throws an error, because it has two sources of error:

1. You define `R` as a local vector inside the `for` loop, and then refer to it outside the loop.
2. `n` is defined as a vector of `Float64` (as well as `R`), but you `push!` vectors like `R` to `n`.

This modification, assigning the result of the formula to a scalar `R`, and pushing it to `n` inside the loop, does work:

```julia
function radius(x,y,a,b,r)
    n=Float64[]
    for (i,m) in enumerate(a)
        R = (((a[i]-x)^2 + (b[i]-y)^2)^0.5 + r[i] ) 
        push!(n, R)
    end
    return n
end

```

```julia
julia> radius(x,y,a,b,r)
5-element Vector{Float64}:
  4.0
 10.517935662402834
 11.512468905280222
 11.201562118716424
 11.363406011768427

```

But it’s more readable, elegant and flexible if you define the function to work on scalars, and then broadcast it (with the “dot notation”):

```julia
julia> radius(x,y,a,b,r) = ((a-x)^2 + (b-y)^2)^0.5 + r
radius (generic function with 1 method)

julia> radius.(x,y,a,b,r)
1×5 Matrix{Float64}:
 4.0 10.5179 11.5125 11.2016 11.3634

```

(Extra note: you are defining `a`, `b`, and `r` as row matrices; if your intention is using one-dimensional vectors, you should define them with commas between values, not spaces.)

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 23, 2022, 6:30pm UTC](https://discourse.julialang.org/t/i-want-my-code-to-display-all-the-five-results-after-each-iteration/79864/4 "2022-04-23T18:30:23Z")

</div>

What if I want only the maximum number out of the 5 values to display. How do I go about that to get only the maximum number displayed please?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 23, 2022, 8:09pm UTC](https://discourse.julialang.org/t/i-want-my-code-to-display-all-the-five-results-after-each-iteration/79864/5 "2022-04-23T20:09:46Z")

</div>

Sounds like you are looking for `maximum`

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 23, 2022, 8:13pm UTC](https://discourse.julialang.org/t/i-want-my-code-to-display-all-the-five-results-after-each-iteration/79864/6 "2022-04-23T20:13:48Z")

</div>

Yes. How do I get only the maximum number out of the 5 solutions displayed please?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 24, 2022, 4:51am UTC](https://discourse.julialang.org/t/i-want-my-code-to-display-all-the-five-results-after-each-iteration/79864/7 "2022-04-24T04:51:21Z")

</div>

If `a` is a vector, `maximum(a)` will return its largest element. In this case, `maximum(radius.( ... ))` should do it.
