0

I have the following code to reverse the digits in an integer:

public class integerReversal {
    public static int reverseNum(int number){
        int reversed = 0;
        int remainder;
        //{I:  ; B: number > 0}
        while (number > 0){
            remainder = number % 10;
            number = number / 10;
            reversed = reversed * 10 + remainder; 
        }
        //{I: ; !B: number == 0}
    return reversed;
    }
    public static void main (String [] args){ 
        System.out.println(reverseNum(1262015 ));
    }
}

My professor tasked us with writing this code and also said to write the loop invariant and loop condition. I understand the loop condition here, I'm just unsure what I should be looking at for the invariant. I realize that it is some condition that will be true at the beginning and end of the while loop, for every iteration, I just do not see what it would be here. Tips would be appreciated.

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
user2049004
  • 157
  • 1
  • 10
  • 2
    It seems your professor wants you to use a different loop such as for, the comment above the loop gives a hint of for loop. – Juned Ahsan Jan 27 '15 at 03:47
  • The comment was my own; my professor specified that we should be using a while-loop to write this. What about the comment indicates that it should be a for loop? – user2049004 Jan 27 '15 at 03:56

2 Answers2

0

In your case reversed >=0 is your loop invariant, as it will always remain true, whether you are breaking loop or not.

  • That was my initial guess but it seemed a bit trivial. Is that usually the case? – user2049004 Jan 27 '15 at 03:55
  • A loop invariant is some condition that holds true for every iteration of the loop. In your loop the predicate reversed >= 0 is a loop invariant, because it's true always. – Aninda Bhattacharyya Jan 27 '15 at 03:56
  • Please accept the answer if it answers your question or let me know if you have any more doubts. Thanks. – Aninda Bhattacharyya Jan 27 '15 at 04:08
  • @AnindaBhattacharyya I'm afraid you don't really understand loop invariants – sprinter Jan 27 '15 at 04:45
  • @sprinter Thanks for commenting on my knowledge, but I believe let's not waste our time and energy in this as I am well aware of my knowledge, and try to focus on our answers. – Aninda Bhattacharyya Jan 27 '15 at 17:26
  • @AnindaBhattacharyya fair point. I was trying to say that I felt your answer was misleading but I agree I didn't express that point well. I added my own answer that I believe is a better expression of the invariant. Looks like the questioner has lost interest in any case! – sprinter Jan 27 '15 at 20:48
0

A loop invariant is a constant expression involving values that are designed to change each iteration.

In your case in each iteration number is the number without the rightmost digit and reversed is the number you have built from the remainders in previous iterations. So I believe the answer is that in every iteration the following expression remains constant:

number * 10 + reverseNum(reversed)

In other words, at any time you can get the original number back by reversing your current solution and adding 10 * the current number.

This sort of invariant is very useful in testing your algorithm - some coders will regularly include an assert statement to test these types of invariants.

There can be several invariants - in your case there are a number of inequalities that remain true.

sprinter
  • 27,148
  • 6
  • 47
  • 78