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
显示 更早的评论
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.')
回答(1 个)
Jan
2017-12-2
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 个评论
Weizhe Tang
2017-12-2
Walter Roberson
2017-12-2
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
2017-12-2
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
2017-12-2
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Hypothesis Tests 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!