# Problem in my code

**URL:** https://discourse.julialang.org/t/problem-in-my-code/105021
**Category:** New to Julia
**Created:** [October 16, 2023, 3:15pm UTC](https://discourse.julialang.org/t/problem-in-my-code/105021 "2023-10-16T15:15:16Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Alex\_Beck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_beck/32/202454_2.png) [@Alex\_Beck](https://discourse.julialang.org/u/Alex_Beck)
#### Post date: [October 16, 2023, 3:15pm UTC](https://discourse.julialang.org/t/problem-in-my-code/105021/1 "2023-10-16T15:15:16Z")

</div>

Hello, can you please correct my code ? I have to do un small presentation and I’ve look for hours… I can’t find anything…  
Here is the error:

 ![Screenshot from 2023-10-16 17-12-26](https://global.discourse-cdn.com/julialang/original/3X/9/8/9836f49f1cb4e8a083995fc254bf4e92455b683c.png)  
and here the file of the code:

```julia
using Plots

function propagation_virus(taux_transmission, personnes_infectees_initiales, population_totale, duree_simulation)
    # Initialisation des tableaux pour le suivi des données
    temps = 1:duree_simulation
    susceptibles = Int[population_totale - personnes_infectees_initiales]
    infectes = Int[personnes_infectees_initiales]
    recuperes = Int[0]

    plots = [] # Stockage des tracés pour créer le GIF

    for jour in 2:duree_simulation
        # Calcul du nombre de nouvelles infections
        nouvelles_infections = Int[susceptibles[jour - 1] * taux_transmission * infectes[jour - 1] / population_totale]

        # Calcul du nombre de récupérés (hypothétiquement, après un certain temps)
        recuperes_jour = Int[infectes[jour - 1] - nouvelles_infections]

        # Mise à jour des tableaux
        push!(susceptibles, susceptibles[jour - 1] - nouvelles_infections)
        push!(infectes, infectes[jour - 1] + nouvelles_infections - recuperes_jour)
        push!(recuperes, recuperes[jour - 1] + recuperes_jour)

        # Création d'un graphique à chaque étape
        p = plot(temps[1:jour], susceptibles[1:jour], label="Susceptibles")
        plot!(temps[1:jour], infectes[1:jour], label="Infectés")
        plot!(temps[1:jour], recuperes[1:jour], label="Récupérés", xlabel="Jours", ylabel="Nombre de personnes", legend=true)
        push!(plots, p)
    end

    return plots
end

# Paramètres de la simulation
taux_transmission = 0.2 # Taux de transmission (0.2 signifie qu'une personne infecte 20% de la population)
personnes_infectees_initiales = 1
population_totale = 1000
duree_simulation = 100

# Exécution de la simulation
plots = propagation_virus(taux_transmission, personnes_infectees_initiales, population_totale, duree_simulation)

# Création du GIF
anim = @animate for p in plots
    p
end

gif(anim, "propagation_virus.gif", fps = 5)

```

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [October 16, 2023, 3:22pm UTC](https://discourse.julialang.org/t/problem-in-my-code/105021/2 "2023-10-16T15:22:39Z")

</div>

This is the same error you were having before. In line 14, you are trying to place a Float into an array of Integers. The quick fix is to remove all of the “`Int`” from your code. Test line-by-line to get a better understanding of what is going wrong rather than running the entire code at once.

> [@Error in my code](https://discourse.julialang.org/t/error-in-my-code/104930/4):
>
> Dividing two integers can result in a non-integer. julia\> 4/3 1.3333333333333333 You are then trying to put that non-integer into an array of integers, but Julia doesn’t know how you want to round it, so it throws an InexactError. julia\> Int[4/3] ERROR: InexactError: Int64(1.3333333333333333) You need to round the result yourself first. julia\> Int[round(Int,4/3)] # round to nearest 1-element Vector{Int64}: 1 julia\> Int[ceil(4/3)] # round up 1-element Vector{Int64}: 2 julia\> Int[fl…

(Your code formatting is better this time! 👍🏻 But you should also copy and paste your error message into a code block between ````` rather than posting a screenshot and try to use a more descriptive title.)

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [October 16, 2023, 3:31pm UTC](https://discourse.julialang.org/t/problem-in-my-code/105021/3 "2023-10-16T15:31:47Z")

</div>

To be even clearer:

```julia
nouvelles_infections = Int[susceptibles[jour - 1] * taux_transmission * infectes[jour - 1] / population_totale]

```

Here you are saying the number of new infections is given by the number of susceptible agents in the population yesterday, multiplied by a transmission rate and the share of infected in the total population. Clearly it is unlikely that this number will be an integer - the transmission rate is probably not an exact integer, and neither will the share of infected in the total population be. So this calculation is bound to create a non-integer number; if you want the number to be integer you have to round it explicitly, so:

```julia
nouvelles_infections = round(Int, susceptibles[jour - 1] * taux_transmission * infectes[jour - 1] / population_totale)

```

Note here I’ve replaced `Int[...]` with `round(Int, ...)`. This will give a single integer number which is then assigned to `nouvelles_infections`. It’s not clear to me why you are doing `Int[...]` here but note that this creates an array of integers with a single number in it, which is not the same as just the number. Later on you are doing:

```julia
push!(susceptibles, susceptibles[jour - 1] - nouvelles_infections)

```

which suggests that `susceptibles` is a vector that you are using to track the number of not-yet-infected over time. Then `susceptibles[jour-1]` is an element of that vector, i.e. likely a single integer, which means `susceptibles[jour - 1] - nouvelles_infections` will fail if `nouvelles_infections` is an array rather than an integer:

```julia
julia> 4 - [2]
ERROR: MethodError: no method matching -(::Int64, ::Vector{Int64})
For element-wise subtraction, use broadcasting with dot syntax: scalar .- array

```
