Question Authors' Forum

Testing implementation of an answer in C++

Testing implementation of an answer in C++

by Richard Lobb -
Number of replies: 0

A correspondent asked:

I am doing the fundamental programming in C++, so the syllabus progresses from non-modular programming (learning to use the vocabulary of C++, control using selection and using loops) into modular programming, ie functions and parameter passing. So, is it possible to evaluate students' answers for specific sections of the program?

Assuming I wanted to evaluate the students' understanding of the while loop (without using a function). 
This answer is expected:

while ( number != 0 ) { remainder = number % 10; revno = revno * 10 + remainder; number = number / 10; }

but not this:

     remainder = number % 10;
     revno = revno * 10 + remainder;
     number = number / 10;
}while ( number != 0 );

even though they are both correct to be used as a block of code to determine if the number is indeed a palindrome.

How to assess the specific block from the whole program like the one in the attached file?
Can we also provide flexibility for the student's answers, for example in line 29, the condition statement is in one line but the block between lines 30 to 33 is also a correct answer (assuming the question give freedom to that part).

My answer:

In all the built-in question types an answer is marked purely according to whether or not it generates the correct output. So if, for example, a student checks for a numeric palindrome by converting the integer to a string, reverses it with the reverse function and checks the two for equality without ever using a loop, that's a correct answer. If you wish to prescribe the method a student can use you need to inspect the source code the student submits. This requires that you customise the question and treat the student answer firstly as a string to be parsed or inspected for the required constructs and then, if it satisfies the conditions, compile and run it. See this documentation for how to do the second part of that process. But it's a much harder process and I suggest avoiding such things until you're more familiar with CodeRunner.

An easier approach is to first get the student to write a function that returns the palindrome of a number. You can preload the answer box with the expected construct and tell students they should add code in the marked places. Yes, some smart ones may cheat by doing it differently but how much to do you care? Most will follow the recommendations.

Then you can set a second question asking the students to use their palindrome function to write the whole program.