Variable DivisionCount is not an integer datatype?

6 visualizzazioni (ultimi 30 giorni)
Lauren Smith
Lauren Smith il 25 Feb 2017
Risposto: dpb il 25 Feb 2017
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

Risposte (1)

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

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by