Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8027101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:42:42+00:00 2026-06-04T23:42:42+00:00

I was wondering if anyone could help me answer this question. It is from

  • 0

I was wondering if anyone could help me answer this question. It is from a previous exam paper and I could do with knowing the answer ready for this years exam.

This question seems so simple that I am getting completely lost, what exactly is it asking for?

Consider the following section of code involving integer variables:

if (i < j) {
    m = i;
} else {
    m = j;
}

By stating an appropriate output condition and then verifying the
correctness of the piece of code, prove that after execution, m is equal to
the minimum of i and j.

I have got the post condition as:
{m = i ∧ i < j ∨ m = j ∧ j < i}

is this correct? and how do you verify this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:42:45+00:00Added an answer on June 4, 2026 at 11:42 pm

    Your post condition is correct. I personally find the following variant (which is equivalent) more natural though:

    (i<j → m=i) ∧ (i≥j → m=j)
    

    To prove that the program satisfies the post condition you do the following.

    1. Note that to make sure the program always satisfies the post condition you should use true as your pre condition.

    2. So you have the following Hoare triple:

      {true}
      if (i < j) {
      
      
          m = i;
      
      } else {
      
      
          m = j;
      
      }
      {(i < j → m = i) ∧ (i ≥ j → m = j)}
      
    3. The postcondition needs to hold at the end of both branches, so (according to the standard weakest precondition rule for conditionals) we have

      {true}
      if (i < j) {
      
      
          m = i;
          {(i < j → m = i) ∧ (i ≥ j → m = j)}     <--.
      } else {                                       |
                                                     |
                                                     |
          m = j;                                     |   copy
          {(i < j → m = i) ∧ (i ≥ j → m = j)}     <--|
      }                                              |
      {(i < j → m = i) ∧ (i ≥ j → m = j)}  ----------'
      
    4. Pushing the formula further up according to the weakest precondition for assignment yields

      {true}
      if (i < j) {
      
          {(i < j → i = i) ∧ (i ≥ j → i = j)}   <---.
          m = i;                                    | m replaced by i
          {(i < j → m = i) ∧ (i ≥ j → m = j)}   ----'
      } else {
      
          {(i < j → j = i) ∧ (i ≥ j → j = j)}   <---.
          m = j;                                    | m replaced by j
          {(i < j → m = i) ∧ (i ≥ j → m = j)}   ----'
      }
      {(i < j → m = i) ∧ (i ≥ j → m = j)}
      
    5. At the top of the true branch we know that i < j, and at the top of the else branch we know that ¬(i < j):

      {true}
      if (i < j) {
          {i < j}                               (1)  <--- added
          {(i < j → i = i) ∧ (i ≥ j → i = j)}   (2)
          m = i;
          {(i < j → m = i) ∧ (i ≥ j → m = j)}
      } else {
          {¬(i < j)}                            (3)  <--- added
          {(i < j → j = i) ∧ (i ≥ j → j = j)}   (4)
          m = j;
          {(i < j → m = i) ∧ (i ≥ j → m = j)}
      }
      {(i < j → m = i) ∧ (i ≥ j → m = j)}
      
    6. What left to show is for any two consecutive assertions, the first assertions implies the second one. (These are usually referred to as “proof obligations”.) We have two such obligations: (1) should imply (2) and (3) should imply (4). This is clearly the case.

      — “qed” 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if anyone could help me with this problem i'm having. I
I was wondering if anyone could help me generate previous/next buttons based on an
Just wondering if anyone could help me with this. I'm new to actionscript, and
I was wondering if anyone could help with this. Let' say I have a
I was wondering if anyone could help me with this. I have to create
I was wondering if anyone could help me with getting this working. I'm using
I was wondering if anyone could help me out with this. I am running
I was wondering if anyone could help me with this problem: I have to
I was wondering if anyone could help me with this minor problem. I want
I was wondering if anyone could help clear up this issue I am having.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.