C# Code Contracts: Array operations -
for university must learn how deal code contracts in c#.
i want use method should return smallest element in array:
public int minarray(int[] minarray)
here got have no idea whether correct:
public int minarray(int[] minarray) { //neither array should empty nor null contract.requires(minarray != null && minarray.length != 0); //save result... int result = contract.result<int>(); //...and check: contract.ensures(contract.forall(0, minarray.length, => minarray[i] >= result)); }
can tell me if right or if wrote nonesense? i'd thankful.
beyond that, next task says:
public int[] posnegarray(int[] posneg)
this should change sign of elements in method (e.g (1, -2,3) (-1,2, -3).
public int[] posnegarray(int[] posneg) { //neither array should empty nor null contract.requires(posneg != null && posneg.length != 0); //save result... int resarray = contract.result<int[]>(); //...and check: contract.ensures(contract.forall(0, posneg.length, => posneg[i] == (-1) * resarray[i] )); int[] returnarray = new int[posneg.length]; for(int j = 0; j < posneg.length; j++) returnarray[j] = posneg[j] * (-1); return returnarray; }
can correct?
contract.result should used within contract.ensures method isn't executed represents result. way using execute method, return default (for int 0) , use default in ensures method isn't expecting. should put result method within ensures this:
contract.ensures(contract.result<int>() >= -1);
Comments
Post a Comment