





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This homework assignment focuses on reasoning about code, requiring students to analyze and evaluate logical implications and hoare triples. It involves analyzing code snippets, determining the strongest postconditions and weakest preconditions, and verifying the correctness of code blocks. The assignment also includes a section on finding input values that produce specific outputs, promoting critical thinking and problem-solving skills.
Typology: Assignments
1 / 9
This page cannot be seen from the preview
Don't miss anything!
CSCI 2600 — Principles of Software
Due: Monday, Feb. 3, 2025, 11:59:59 pm
This homework is different from the previous homework. It does not involve any Java coding. Rather, you will answer some questions involving reasoning about code. Follow the directions in the version control handout for cloning your hw01 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/s25/csci2600/hw01/RCSID where RCSID is your RCS ID. Submit your answers in a single .PDF file named hw1 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 file to Submitty. Follow the directions in the version control handout for adding and committing files.
Directions:
Unless otherwise indicated, assume that all referenced numerical variables are defined as integers, and that integer overflow or underflow will not occur. In your answers, please use standard logical symbols ∧ and ∨ for “and” and “or” respectively. Use = for equivalence (comparing equality) in logical statements. Note that braces ({ and }) are used in this homework to enclose conditions, as well as to define blocks of code. Make sure you distinguish between these two different uses of braces based on the context. If no precondition is required for a code sequence, simply write {␣true␣} to denote the trivial precondition. You must show work for all problems in this homework in order to get credit. If you provide just the final answer but fail to show work, you will get a zero, even if the answer is correct. All code is in Java and assumes the use of standard Java library.
Problems
Part (a). State whether the following implication chains are true. Note that the implication arrow in this part is pointing to the left, i.e., the rightmost condition would be the strongest, if the chain is true. For any chains that are not true, indicate all implications that make those chains false, and provide a counterexample for each false implication.
|·| is the absolute value operator. E.g., this chain { x > 0 } ← { x is odd } ← { x is prime } ← { x = 37 } is false because the following implications are false:
{ x is odd } ← { x is prime }. Counterexample: x = 2 which makes this implication { false } ← { true } which is false. { x > 0 } ← { x is odd }. Counterexample: x = −1 which makes this implication { false } ← { true } which is false. (1) { x is a Monday } ← { x is a day in January 2025 } ← ← { x is a Monday in January } ← { x is Monday, January 27, 2025 } (2) { u = 10k ∧ v = y + 7 ∧ u + v = 7k } ← { 10x + y is divisible by 7 } ← ← { x - 2y is divisible by 7 ∨ x - 2y = 0 } ← { x = 60 ∧ y = 9 } ← ← { 10x + y = 609 } (3) { true } ← { |x| = x } ← { x > 0 } ← { 10 < x < 50 ∧ x < 0 } ← { false }
Part (b). Order conditions from the weakest to the strongest by showing the longest impli- cation chain of the form A ← B ← C ← D... (where A, B, C, D,... are given conditions, ← is implication). If total ordering by strength cannot be established, show a partial ordering. Prove by showing all necessary implications. If any implication is false, provide a counterexample. You must show all work in order to get full credit. Unless otherwise stated, assume all referenced variables are defined as integers.
(1) { 6 ≤ k < 6 ∧ k = 6 } { false } { k = 6n } { 12 ≤ k ≤ 12 } (2) { x = 3k + 3 ∧ x is even } { x is divisible by 6 } { y = the sum of digits of x ∧ y % 3 = 0 ∧ x = 2k } { x = x + 1 ∧ y = 12 } (3) Assume result is a double. { result =
x } { |result^2 - x| ≥ 0.0 } { |result^2 - x| ≤ - 10 −^10 } { |result^2 - x| ≤ 0.001 }
A, B, C, D, E, and F are logical conditions (logical formulas). The following are true: A → B (A implies B, i.e., A is stronger than B) B → C C → D D → E E → F E → G { B } code { E } Indicate whether the following Hoare triples are valid or possibly invalid. If possibly invalid, prove by giving an example.
(1) {E} code {B} (2) {B} code {E} (3) {C} code {D} (4) {E} code {E}
For each code snippet with the given precondition, find the strongest postcondition by inserting the appropriate condition in each blank. The first intermediate condition in part (1) is supplied as an example. Please simplify your answers as much as possible. Assume all referenced variables are defined as integers. Copy all code to your answer file and fill in the blanks. Carry all variables forward. Show all work.
(1) { z = 0 } x = 10; { x = 10 ∧ z = 0} y = y - x; {____________________________________________________________} z = x - y; {____________________________________________________________} y = 0; {____________________________________________________________} z = 2 * k; {____________________________________________________________}
(2) { |x| > 4 } y = x; {____________________________________________________________} x = -x * y; {____________________________________________________________} x = x + y; {____________________________________________________________} (3) { xy = 0 } if (x > 0 || y > 0) { {____________________________________________________________} y = y * x; {____________________________________________________________} } else { {____________________________________________________________} x = x + y; {____________________________________________________________} } {____________________________________________________________}
Find the weakest precondition of each code sequence by inserting the appropriate condition in each blank. The first intermediate condition in part (1) is supplied as an example. Please simplify your answers as much as possible. Assume all referenced variables are defined as integers.
(1) Use the wp() notation shown in the example for all conditions in this part. {____________________________________________________________} x = x + k; { wp("z = 2 * y + x;", z ≤ 10) } = { 2y + x ≤ 10 } = { 2y ≤ 10 - x } = { y ≤ 102 − x } z = 2 * y + x; { z ≤ 10 } (2) Use the wp() notation for all conditions in this part. {____________________________________________________________} y = x; {____________________________________________________________} x = -x * y; {____________________________________________________________} x = x + y; { x > 0 }
For each block of code, fill in all the conditions, then use them to state whether the precondition is sufficient to guarantee the postcondition. If the precondition is insufficient, explain why.
Hint: Use backward reasoning to find the weakest precondition that guarantees the postcondition and see if the given precondition is strong enough to guarantee the postcondition. In other words, is the given precondition not stronger than the weakest precondition?
Copy all code to your answer file and fill in the blanks. Show all work. Assume all referenced variables are defined as integers.
(1) { x < 2 } {____________________________________________________________} z = x * 2; {____________________________________________________________} w = -z; {____________________________________________________________} w = w - 1; {w > 1} Sufficient or Insufficient: __________________________________________________ (2) { x = y ∧ y > 0 ∨ y ̸= x } {____________________________________________________________} if (x == y) { {____________________________________________________________} x++; {____________________________________________________________} } else { {____________________________________________________________} x = y + 2; {____________________________________________________________} } { x > y ∧ y > 0 } Sufficient or Insufficient: __________________________________________________
Find all possible values of inputs that cause the code below to produce the output observed. Assume all variables are ints and have been properly declared and initialized. You need to apply a reasoning technique, not just “see” or “guess” the answer or run experiments with the code. Remember that you need to find all combinations of inputs, not just one. Show all work.
if ( x >= y - b ) { y = y + b * x ; b = b + x + y ; } else { b = 1 - x ; x = y - x ; } System. out. printf ( " % b % b % b \ n " , b < 0 , x > y , y < 0) ;
prints
true false true