Hello,

There's a no home work policy but since you've obviously tried, here it 
goes.
The main problem with your function is that you are testing outside the 
loop. Another problem is that your function doesn't return a value. Both 
problems are corrected. I also include a function that does the same in 
a vectorized way. As you can see with the tests below, they produce the 
same output.


stops <- 25
passengers <- 0
register <- numeric(25)

bus <- function(register,stops,passengers) {
     register[1] <- passengers
     for (i in 1:stops) {
         passengers <- passengers + sample(0:6, 1)
         register[i] <- passengers
         # Para ir viendo cuanto hay:
         cat('Stop', i, 'hay', passengers, 'passenger\n')
     }
     # If the bus isn't full:
     if (passengers >= 44) {
         # Adjustment if it reaches 44 passenger, before the 25th stop:
         register[register >= 44] <- 44
         cat('Full bus!\n') # Warning message...
     }
     register  # return value
}

bus2 <- function(register,stops,passengers){
     passengers <- sample(0:6, stops, replace = TRUE) # All at once
     register <- cumsum(passengers)  # running sum of passengers
     register[register >= 44] <- 44  # adjust for overflow
     register
}

set.seed(3862) # To make reproducible runs
r1 <- bus(register,stops,passengers)
set.seed(3862)
r2 <- bus2(register,stops,passengers)
identical(r1, r2)


Note that in the corrected version of your function the if test is no 
longer needed.

Hope this helps,

Rui Barradas
Em 18-12-2012 20:05, Wanda Iriarte escreveu:
> Hi! I am doing a course about the R software and I have a couple of doubts.
>
> In one of the tasks we were asigned, we have to perform a function that
> simulates a bus' trip with 25 stops. In each of those stops, between 0 and
> 6 people can go on board. And when the total of passengers reaches 44, no
> more people can go on board.
>
> stops <- 25
>
> passengers <- 0
>
> register <- numeric(25)
>
> bus <- function(register,stops,passengers) {
>
> register[1] <- passengers
>
> for (i in 1:stops) {
>
> passengers <- passengers + sample(0:6,1)
>
> register[i] <- passengers
>
> }
>
>    # If the bus isn't full:
>
>    if (passengers >= 44) {
>
>      # Adjustment if it reaches 44 passenger, before the 25th stop:
>
>      register[i:stops] <- 44
>
>      cat('Full bus!\n') # Warning message...
>
>      break
>
> } else {
>
>    # If the loop doesn't stop, a new register must be added:
>
>    register[i] <- passengers
>
> }
>
>    # Para ir viendo cuánto hay:
>
>    cat('Stop', i, 'hay', passengers, 'passenger\n')
>
> }
>
> plot(register, xlab='Stop', ylab='No. of passengers')
>
> The parts of the script that are marked in sky blue, are the ones that we
> have to fill up, so on of those should be my mistake.
>
> I would be very glad, if anyone can detect my error, as I have been many
> days stuck on this part and my deadline is tomorrow.
>
> Thx a lot in advance to everyone.
>
> Wanda
>
> BTW: Sorry for my English! My native language is Spanish! :)
>
>       [[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to