Flow Control and Loops

Hi everyone!
Is there anyone that can help me?

1- If I want to write a function(x) that takes (in this order) a and b as arguments and that returns the number of required divisions. (Example: a = 5,b = 2. Then we have: 5/2 = 2.5 (1 division), 2.5/2 = 1.25 (2 divisions), 1.25/2 = 0.625 (3 divisions), 0.625/2 = 0.3125 (4 divisions), 0.3125/2 = 0.1562 (5 divisions, result still not smaller than 0.1) and finally 0.1562/2 = 0.078125 (6 divisions, now the result is smaller than 0.1). In this case, the answer would be 6)

2- How many times do you have to roll the dice until you are allowed to start? The trial when you get 6 also counts. So if you roll for example 4, 2, 3, 1, 6 the number of trials and the answer would be 5.
Write a function(x) , that takes no input argument and returns the number of tries you need to obtain a 6.

Thanks

Hi, welcome!

Homework inspired questions are welcome here, but you have to tell us what have you tried so far? what is your specific problem? We are more inclined towards helping you with specific coding problems rather than doing your work for you.

For guidelines about how to properly ask homework related questions here, please read our homework policy.

we trying to do this :slight_smile: for the first

divCount <- function(a, b) {
while ((a/b) > 0.1){
count = 0
return(count + 1)
}
}

divCount(5, 2)

A while loop has a logical condition and the loop keeps repeating until the condition changes to FALSE. It doesn't make sense to have the condition never change. In your case, if the ratio is greater than 0.1 the function always returns 1.0. If it is less than 0.1, the loop is never executed and the function returns a null result.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.