Colorado State University

Recitation R15 - Mathematical Proofs
Spring 2014

CS160: Foundations in Programming


The purpose of this lab is to:

Math Homework

Students will present the answers to each of the three proofs from Math Homework 2, linked here. Show your paper homework to the TA at the beginning of class to get credit for the assignment. Incomplete homeworks will only receive partial credit.


Create a text file called R15.txt and solve the following pre-condition, post-condition, and loop invariant problems:

  1. Is the following program segment correct with respect to the pre-condition y >= 5 and the post-condition z >= 65? Please give the reason why or why not.
  2. int x = 13;
    z = x * y;
    if (y > 0)
    	z++;
    else
    	z--;
    	
  3. Given the pre-condition -2 <= y < 4, what is the post-condition for z in the code segment shown below?
  4. int z = 0;
    if (y < 0)
    	z = y * -y;
    else if (y == 0)
    	z = 0;
    else
    	z = y * y;
    	
  5. Given the post-condition -4 <= z <= 4, what is the pre-condition for x in the code segment shown?
  6. z = x * x + 2 * x - 4;
    	
  7. What is the pre-condition for x, if the post-condition is z >= 25 for the code segment shown?
  8. y = 15;
    z = 100 - x - (2 * y);
    	
  9. What are the loop invariants for all variables in the code segment shown?
  10. a = 5;
    b = 2;
    c = -3;
    while (c < 0) {
    	b = b + 2 * a;
    	a = b / 3;
    	c++;
    }
    	
  11. Given the pre-conditions a = 5 and b = -3, what are the loop invariants for all variables except the loop counter in the code segment shown?
  12. c = 2;
    for (int i = -2; i < 3; i++) {
    	c += a-- + (b * i);
    }
    	
The answers will be discussed at the end of class.
Show your work to the TA and turn in R15.txt to RamCT for credit on this recitation.

© 2014 CS160 Colorado State University. All Rights Reserved.