How do I check students use variables instead of hard-coded numbers in their scripts?

7 views (last 30 days)
For example - writing a script to calculate the area of a triangle; this is the template:
b = 15;
h = 11;
area = %....
Of course the solution I want is area = b*h/2;. How can I check the student hasn't submitted:
area = 15*11/2;
If I only check the value of the variable the above code would pass the test... even though it's not exactly what I was asking.
Any ideas?
  4 Comments
Stephen23
Stephen23 on 2 Feb 2017
Edited: Stephen23 on 2 Feb 2017
Cody is not a good way to teach good code habits.
"I cannot expect complete beginners to know how to use functions straight away"
Why not? They use them all the time: sin(pi), and defining a function is equally trivial. Personally I would get them to avoid scripts, which cause more problems than they solve.
Adam
Adam on 2 Feb 2017
Edited: Adam on 2 Feb 2017
I never learned Matlab at university or anywhere like that, my learning was entirely from scratch in my job so it's hard to say what should be expected of complete beginners, but a function is one of the most trivially fundamental concepts in pretty much any language.
You can turn any short script into a function very easily and even longer scripts too, depending how particular you want to be. It could be taught in 5 minutes in its simplest form.
I would have thought this should be the first thing that gets taught. I still use scripts for doing quick testing of new ideas, but even then I often end up turning it into a function, if only because I don't want workspace contamination.

Sign in to comment.

Answers (3)

KSSV
KSSV on 2 Feb 2017
You may run the code and see for the variables in work space...Like
variablesInCurrentWorkspace = who
numVariables = length(variablesInCurrentWorkspace)
  1 Comment
mcarpe
mcarpe on 2 Feb 2017
Edited: mcarpe on 2 Feb 2017
Thanks, but this does not solve anything. The following solution would pass your test:
b = 15;
h = 11;
area = 15*11;

Sign in to comment.


Jan
Jan on 2 Feb 2017
Obviously reading the source code would be a trivial and perfect solution. You cannot expect any automatic method to be as efficient than a visiual control by a human. This would even reveal further problems.
You've used the tag "cody coursework". This is an established service. My personal opinion is, that this is a very bad method for teaching. On one hand the teacher has no impression of what the students exactly do. On the other hand it has been a lot of fun to get the Cody system to accept a solution by cheating.
  2 Comments
mcarpe
mcarpe on 2 Feb 2017
Well, I'm not planning to give them automatic marks from Cody. This should only help them getting a better solution and improve their coding. That's the whole point. If a student submit an answer that passes the test I'd like for them to be confident that their is a correct answer. In the example I've made it is not, and they won't realise it until they get the final mark.
Reading the source code is a perfect solution when you have 1 problem and 20 students, maybe. When you start having tens of different problems and ~300 students it gets a bit tricky...
Jan
Jan on 2 Feb 2017
@mcarpe: I agree. Then the problem is neither Matlab nor the automatic control of code, but the huge number of students per teacher, or in other words: money.
I have ideas about building subgroups of 15 students, who control their programs at first by their own and submitting solutions as groups. Then you can teach the 20 group leaders how good programming practices. Finally its their responsibility to distribute this knowledge to the others. But, well, I admit that this does not have any connection to your actual question.
Remember that others have failed with searching automatic methods to assess code: https://en.wikipedia.org/wiki/Halting_problem . In the times of expensive CPU time, a professor asked for an automatic test, which detects infinite loops. The solution was fundamental: This cannot work.
For your case: You could overload the operators and check with a C-Mex function the type of the arguments scope. But this wil fail for tricks like:
area = eval('15')*11/2;
or:
a = 15;
area = a(1)*11/2;
or:
area = builtin('mult', 15, 11/2);
Therefore I'm convinced that this approach cannot work reliably, but come back to the meta answer: Start at reducing the number of students per teacher.

Sign in to comment.


drummer
drummer on 18 Aug 2020
Edited: drummer on 18 Aug 2020
Well, I think that if your students would be cheating the way you mentioned, you could check both the variable value and if the number of lines in their codes are higher than one.
There is this solution to count the number of lines in the code, excluding comments lines as well as the empty ones.
You can make the solution as a function returning the number of the lines, like this:
file = 'studentsCode.m';
nLines = lineCounterFcn(file); % this is a random name to the function using the ...
% aforementioned solution
if nLines > 1 && area == (15*11)
fprintf('ok, grab a beer');
else
fprintf('screwed.')
end
Cheers

Categories

Find more on Historical Contests in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!