Question: method BadNbr ( a: array ) returns ( bn: int ) requires a ! = null ensures ( a . Length 2 = = >
method BadNbra: array returns bn: int
requires a null
ensures aLength bn If the array is too small, return
&& bn bn aLength If a bad neighbour exists, bn is a valid index
&& bn forall i :: i aLength
ai ai aLength; No bad neighbours
Base case: if the array length is less than no bad neighbours possible.
if aLength
return ; No bad neighbours in arrays with fewer than elements.
var n : aLength;
Check the wraparound pair last and first elements
if an a
return ; The right neighbour of the first element index is bad
Check for bad neighbours in consecutive pairs
var i :;
while i n
invariant i n
invariant forall j :: j i aj aj
if ai ai
return i ; The right neighbour of the current element index i is bad
i : i ;
return ; No bad neighbours found
method TestBadNbr
Test case : Empty array
var arr : new int;
var res : BadNbrarr;
assert res; No bad neighbours, so return
Test case : Array with one element
var arr : new int;
arr :;
var res : BadNbrarr;
assert res; No bad neighbours, so return
Test case : Array with two elements where sum is negative
var arr : new int;
arr :;
arr :;
var res : BadNbrarr;
assert res; Right neighbour index is bad
Test case : Array with three elements and a bad neighbour pair
var arr : new int;
arr :;
arr :;
arr :;
var res : BadNbrarr;
assert res; Right neighbour index is bad
Test case : Array with four elements and a wraparound bad neighbour pair
var arr : new int;
arr :;
arr :;
arr :;
arr :;
var res : BadNbrarr;
assert res; Right neighbour index is bad wraparound pair
I changed my code to use the wrap around method but I still getting errors that the assertion will not hold
eng dafny verify exdfy
exdfy: Error: assertion might not hold
assert res; No bad neighbours, so return
exdfy: Error: assertion might not hold
assert res; No bad neighbours, so return
exdfy: Error: assertion might not hold
assert res; Right neighbour index is bad
exdfy: Error: assertion might not hold
assert res; Right neighbour index is bad
exdfy: Error: assertion might not hold
assert res; Right neighbour index is bad wraparound pair
Dafny program verifier finished with verified, errors
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
