Variable DivisionCount is not an integer datatype?

1 view (last 30 days)
Hello, I am using Cody Coursework for this code, but I keep getting an error that states 'Variable DivisionCount should be integer datatype. I'm not sure what is wrong with my code.
Question:: 'The test suite assigns a random number to the variable Number. Write a script that uses a while loop to repeatedly divide this number by 7 until the value remaining is less than 1. Assign the result to the variable WhatsLeft. Keep track of the number of divisions required and assign to the integer variable DivisionCount.'
What I have::
count = 0
while Number>1
Number = Number/7
count = count + 1
end
WhatsLeft = Number
DivisionCount = count

Answers (1)

dpb
dpb on 25 Feb 2017
All variables are by default type double in Matlab unless you make them something else; simple assignment won't do it, even if the value is integer-valued.
Don't know why it needs to be, other than the rules of engagement say so, but
DivisionCount=int32(count);
will do it. You could, of course initialize it and use it instead of the temporary count.
Also, note that the requirement is for result to be <1; your loop will stop if initial value is a multiple of 7 at 1.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!