Variable DivisionCount is not an integer datatype?
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
dpb
2017-2-25
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!