CSCI 2600 Homework 2

.pdf

School

Rensselaer Polytechnic Institute *

*We aren’t endorsed by this school

Course

2600

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

7

Uploaded by LieutenantGrasshopper913

CSCI 2600 — Principles of Software Homework 2: Reasoning About Loops Due: Tuesday, Feb. 13, 2024, 11:59:59 pm Introduction In this assignment you will prove correctness of loops using the techniques we discussed in class. Submission Instructions Follow the directions in the version control handout for cloning your hw02 git repo.The URI that you will need to use to clone your personal repo for this homework would have the form of https://submitty.cs.rpi.edu/git/s24/csci2600/hw02/RCSID where RCSID is your RCS ID. Submit your answers in a single .PDF file named hw2_answers.pdf in the answers/ directory of your repository. Submit your Dafny code as problem1.dfy , problem2.dfy , problem3.dfy , and problem4.dfy files in the answers/ directory of your repository. Submit your answers in a single .PDF file named hw2_answers.pdf in the answers/ directory of your repository. You MUST type up your answers. Handwritten solutions will not be accepted or graded, even if they are scanned into a PDF file. We recommend using LaTeX . If you have never used LaTeX, take a look at this tutorial . Be sure to commit and push the files to Submitty. Follow the directions in the version control handout for adding and committing files. Important: You must click the Grade My Repository button for you answers to be graded. If you do not, they will not be graded and you will receive a zero for the homework.
Problems All answers should be placed in answers/hw2 answers.pdf except for Dafny files which should be separate files with names as given in each problem description. Problem 1 (8 pts.): Sum of natural numbers Below we give, in Java syntax, the sumn method which computes the sum of the n natural numbers from 1 to n . For example, sumn (0) == 0, sumn (1) == 1, sumn (2) == 3, and sumn (6) == 21. // Precondition : n > = 0 int sumn( int n) { int i = 0 , t = 0; while ( i < n) { i = i + 1; t = t + i ; } return t ; } // Postcondition : t = n (n + 1) / 2 a) Find a suitable loop invariant. (1 pt.) b) Show that the invariant holds before the loop (base case). (1 pt.) c) Show by induction that if the invariant holds after k-th iteration, and execution takes a k+1-st iteration, the invariant still holds (inductive step). (2 pts.) d) Show that the loop exit condition and the loop invariant imply the postcondition t = n ( n +1) / 2. (1 pt.) e) Find a suitable decrementing function. Show that the function is non-negative before loop starts, that it decreases at each iteration and that when it reaches 0 the loop is exited. (1 pts.) f) Implement the sum of natural numbers in Dafny. (2 pts., autograded) Comment out method Main in your Dafny code before you submit your code on Submitty. Method that implements the sum of natural numbers must be named sumn and have the following header: method sumn(n: int) returns (t: int) Make sure to include the precondition and the postcondition, as well as your invariant and the decrementing function. Verify your code with Dafny before submitting. Submit your Dafny code as a file named problem1.dfy in the answers/ folder.
Problem 2 (15 pts.): Loopy square root Below we give, in Dafny syntax, the square root method which should be computing the square root of a number. method loopysqrt(n:int) returns (root:int) requires n >= 0 ensures root * root == n { root := 0; var a := n; while (a > 0) //decreases //FILL IN DECREMENTING FUNCTION HERE //invariant //FILL IN INVARIANT HERE { root := root + 1; a := a - (2 * root - 1); } } a) Test this code by creating the Main() method and calling loopysqrt() with arguments like 4 , 25 , 49 , etc. to convince yourself that this algorithm appears to be working correctly. In your answer, describe your tests and the corresponding output. (1 pt.) b) Yet, the code given above fails to verify with Dafny. One of the reasons for this is that it actually does have a bug. More specifically, this code may produce the result which does not comply with the specification. Write a test (or tests) that reveals the bug. In your answer, describe your test(s), the corresponding outputs, and the bug that you found. Also, indicate which part of the specification is violated. (1 pt.) c) Now find and fix this bug. Note that there might be several different ways of fixing the bug. Use the method that you think would be the best. You are not allowed to change the header of loopysqrt() or add, remove, or change any specifications or annotations. Do not worry about Dafny verifying the code for now, just fix the bug and convince yourself that loopysqrt() is now correct. You are also required to keep the overall algorithm the same as in the original version of the code. In your answer, describe how you fixed the bug and show the output of the same tests you ran before after fixing the bug. (2 pts.) d) Update the specification of loopysqrt() to match the way you fixed the bug. If you changed the postcondition, make sure that it is the strongest possible postcondition. In your answer, describe your changes and explain why they were necessary. (1 pt.) e) Does your Dafny code verify now? Why or why not? If it doesn’t verify, does it mean that your code still has bugs in it? (1 pt.) f) If your Dafny code doesn’t verify, uncomment invariant and/or decreases annotations and supply the actual invariant and/or decrementing function. Make sure your code now
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help