"Variable 'DivisionCount' should beinteger datatype."? integer datatypes?

So I am stuck on this computer feedback I received on my homework. Sorry for all the words... this is what I had to do.
"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."
This is my code:
DivisionCount = 0
while Number > 1
Number = Number / 7
DivisionCount = DivisionCount + 1
end
WhatsLeft = Number
DivisionCount = DivisionCount
I get this comment: "Variable DivisionCount should beinteger datatype." I don't know how to fix this.

Antworten (2)

Lots of them.
help uint8
help uint16
help uint32
help uint64
help int8
help int16
help int32
help int64

7 Kommentare

How do I use the int command to make the DivisionCount variable an integer datatype? Do you mean to put DivisionCount in like int8(DivisionCount) or something else? What I did gave me this:
Integers can only be combined with integers of the same class, or scalar doubles
Jan
Jan am 24 Feb. 2017
Bearbeitet: Jan am 24 Feb. 2017
@Michael: Start with
DivisionCount = uint64(0)
Then the rest of the code will work.
I added that to the first line outside the loop. The other error doesn't show, but now I got this. "Error using - Integers can only be combined with integers of the same class, or scalar doubles."
You should be able to do it on the final line. I don't know why Matlab coursework would specify that a counter needs to be an integer data type. Sure, we all know a counter will be an integer and in C++ we would declare it as such, but in Matlab since we don't declare variables with a type every number is a double by default. Trying to crowbar integer datatypes into maths generally leads to a mess like this so most of us just accept that we have a double data type containing something that we know should always be integer valued. This does lead to some unpleasant surprising when doing e.g. indexing, but less so than trying to force a load of maths into integer types.
As an aside, when passing such numbers around between functions, I often make use of
validateattributes( myVariable, { 'double' }, { 'integer' } )
It is counter-intuitive, but useful, though way beyond what you are being asked for!
But since your coursework does require this then cast it at the end so you don't have to deal with issues of integer maths.
Should that look something like this? I get this response again. "Variable DivisionCount should beinteger datatype."
Number = Number
DivisionCount = 0
validateattributes( DivisionCount, { 'double' } , { 'integer' } )
while Number > 1
Number = Number / 7
DivisionCount = DivisionCount + 1;
end
WhatsLeft = Number
Adam
Adam am 28 Feb. 2017
Bearbeitet: Adam am 1 Mär. 2017
No, just add
DivisionCount = int16( DivisionCount );
at the end (or any of the others that John D'Errico posted)
This code worked for me:
DivisionCount = 0;
while Number > 1
Number = Number / 7;
DivisionCount = DivisionCount + 1;
end
WhatsLeft = Number;
disp(WhatsLeft)
DivisionCount = uint8(DivisionCount);
disp(DivisionCount)

Melden Sie sich an, um zu kommentieren.

Jonathan Truitt
Jonathan Truitt am 28 Feb. 2017

0 Stimmen

They making while two loops. One for your first to variables. The second for your last variable. The last loop will need a break. Also, make your first line of code DivisionCount = uint(0).
Hope that helps!

Gefragt:

am 24 Feb. 2017

Bearbeitet:

am 1 Mär. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by