help with c++ assertions

This is not my homework -- I'm just practicing. I can't seem to wrap my mind around this assertion concept. Help would be appreciated! thanks

1
2
3
4
5
6
7
8
9
10
1) Determine the pre-condition for x that guarantees the post-condition about y
{ _______ }
if (x > 0)
    y = x;
else
    y = -x;

{ y >= 0 && |y| == |x| }
2) This code is the same as has been seen before, but the post-condition is different.                    
Describe why this post-condition is better for verifying the code than just { y >= 0}.
You need to specify the range of x values that will lead to the post-condition being true.

The post-condition says that y is greater-than or equal to zero, and that the absolute value of y equals the absolute value of x.

For the code, any value of x works.

Unless you're needing to worry about signed integers with -ve ranges bigger than +ve. Then you need to make sure |-x| == |x|

Or something.

Jim
Topic archived. No new replies allowed.