Problem Description 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
Show older comments
I cannot find any error in my code code:
function [WhatsLeft] = G(Number)
count = 0;
while Number >= 1
Number = Number/7; % divides given number by 7
count = count + 1;
end
WhatsLeft = Number; % assigning result to Whatsleft
DivisionCount = int32(count);
end
But the test code just said that solution not found test code :
%first test case (visible number)
Number = 12345.678;
reference.solution
y_correct1 = WhatsLeft;
y_correct2 = DivisionCount;
clear WhatsLeft DivisionCount
run('solution')
%test if variables exist, and are named correctly
assert(isequal(exist('WhatsLeft','var'),1),['The variable, WhatsLeft, '...
'has not been created. '...
'Check spelling and capitalization.'])
student_sol1 = WhatsLeft;
assert(isequal(exist('DivisionCount','var'),1),['The variable, '...
'DivisionCount, has not been created. '...
'Check spelling and capitalization.'])
student_sol2 = DivisionCount;
%test datatype
assert(isa(student_sol2,'integer'),['Variable DivisionCount should be'...
'integer datatype.'])
%test accuracy
test1 = abs(y_correct1 - student_sol1);
assert(le(test1,1e-4),'Value of WhatsLeft is numerically incorrect.')
test2 = abs(y_correct2 - student_sol2);
assert(le(test2,1),'Value of DivisionCount is numerically incorrect.')
Answers (1)
Jan
on 2 Dec 2017
The test suite seems to expect a script, not a function. Omit the line:
function [WhatsLeft] = G(Number)
and the trailing
end
and try it again.
4 Comments
Weizhe Tang
on 2 Dec 2017
Walter Roberson
on 2 Dec 2017
You have created your count as a different kind of integer than the code is expecting. For example you would get this if the code was expecting uint8 or int16.
Jan
on 2 Dec 2017
Why do you use "int32(count)"? I do not see a reason to convert this to an UINT32, just leave it a double.
Walter Roberson
on 2 Dec 2017
Jan, notice this part of the test suite:
assert(isa(student_sol2,'integer'),['Variable DivisionCount should be'...
'integer datatype.'])
This might have been a misunderstanding on the part of the creator of the test suite as to what isa(x, 'integer') tests, or it might reflect instructions that were not shown to us.
Categories
Find more on Hypothesis Tests 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!