Variable DivisionCount is not an integer datatype?

조회 수: 3 (최근 30일)
Lauren Smith
Lauren Smith 2017년 2월 25일
답변: dpb 2017년 2월 25일
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

답변 (1개)

dpb
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by