# Calculate Malthusian Pop   only 2 sig fig produce offspring rate
malpop = function(prob,pop0,nyears){
  totpop=pop0
    for (i in 1:nyears) {
        outcome=sample(1:100,totpop,replace=T) #p integers from 1:100
        addchild=sum(outcome<=prob*100) # if less than prob add child
        totpop=totpop+addchild
    }
    return (totpop)
}
    
malpop(0.04,50,10)
## [1] 81
# Can't Use malpoplist!!!! Stick with using mal pop function inside
malpopsim = function (prob,pop0,nyears,repeats){
    totalpop = rep(0,repeats)
    for (i in 1:repeats){
        totalpop[i] = malpop(prob,pop0,nyears)
    }
    avgpop=mean(totalpop)
    sdpop= sd(totalpop)
    return( c("mean:",avgpop,"std dev:",sdpop) )
}

#PART B run simulation 1000 times
malpopsim(0.04,50,10,1000)
## [1] "mean:"            "73.977"           "std dev:"        
## [4] "5.77180948964749"
#Show pop of each year
malpoplist = function(prob,pop0,nyears){
    totpop=rep(0,nyears+1)
    totpop[1]=pop0
    for (i in 1:nyears) {
        outcome=sample(1:100,totpop[i],replace=T) #p integers from 1:100
        addchild=sum(outcome<=prob*100) # if less than prob add child
        totpop[i+1]=totpop[i]+addchild
    }
    return (totpop)
}

malpoplist(0.04,50,10)
##  [1] 50 50 51 56 60 62 65 67 69 70 74