Question

In: Computer Science

Write the loop invariant for the following program the computes quotients (q) and remainders (r) of...

Write the loop invariant for the following program the computes quotients (q) and remainders (r) of “a” divided by ”d”. Then prove all three cases (Initialization, Maintenance, Termination). Assume “a” and “d” are positive integers.

r=a

q=0

while r>=d

            r=r-d

            q=q+1

Solutions

Expert Solution

r=a

q=0

while r>=d

         r=r-d

         q=q+1

A loop invariant is an expression that is true through all iterations. But it should also lead to the post-condition being true when the loop terminates

Intuitively, You would want the invariant to be r = a-q*d because that's what division is and that's what guarantees that you'll get the post-condition ( q=quotient, r=remainder) when the termination condition ( r < d ) is true.

  • Loop Invariant:

    • Initialization: prior to the iteration of the loop we have r=a and q=0 and the loop invariant holds for that condition as
      r = a-0*d=a

    • Maintenance: To see that each iteration maintains the loop invariant, we check that if the loop has ran k times and in its k+1 iteration then the value of r will be r = a-k*d and according to our loop condition the value of q will be equal to no of iterations and hence its value will be equal to k, now if the condition for the loop still satisfies the value of r >= d and the loop variant still holds as r=a-k*d=a-q*d

    • Termination: At termination, r<d . By the loop invariant, the value of r = r-q*d which is at this time is equal to k and for k+1 the value of (k+1)*d > a and the value of r is lesser then the value of d and hence it is the reminder for this division algorithm and the value of q which is equal to k is the quotient of this division algorithm


Related Solutions

Find a loop invariant (I) for the following while loop with the post-condition {Q: S=1}, where...
Find a loop invariant (I) for the following while loop with the post-condition {Q: S=1}, where S is an integer and the operator / represents an integer division. Show your work step by step to receive full credit. while (S > 1) do S = S / 2; }
According to the loop invariant theorem, a valid loop invariant ?(?)I(n) must have the property that...
According to the loop invariant theorem, a valid loop invariant ?(?)I(n) must have the property that Select one: a. ?(?)=0I(n)=0 after a finite number of steps. b. After the loop, ?→?(?)P→I(N) where ?P is the postcondition. c. ?(0)→?I(0)→Q, where ?Q is the precondition. d. None of the other answers are correct
Write the following java program. Desc: The program computes the cost of parking a car in...
Write the following java program. Desc: The program computes the cost of parking a car in a public garage at the rate $5.00/hour. The client will always be charged for whole hours. For example, if a car parked for 2 hours and 1 minute, the client will be charged for 3 hours. Input: User inputs the entry time and exit time in 24-hr clock format (hh:mm) Output: The enter and exit times, the length of time the car is parked...
You are to write a program in C to do the following in a loop for...
You are to write a program in C to do the following in a loop for the KL46Z . Prompt the user for a positive integer greater than 1 and sanity-check the input. If the number is a prime number, it is to be printed on a new line in red text. If the number is evenly divisible by 7, it is to be printed on a new line in green text. If the current number is evenly divisible by...
Write a C++ program to construct the truth table of P ∨¬(Q ∧ R) If you...
Write a C++ program to construct the truth table of P ∨¬(Q ∧ R) If you could include comments to explain the code that would be much appreciated!!! :) Thank you so much!
Write a Python program that computes the income tax for an individual. The program should ask...
Write a Python program that computes the income tax for an individual. The program should ask the user to enter the total taxable income for the year. The program then uses the tax bracket (as shown below) to calculate the tax amount. This is based on a progressive income tax system which is how income tax is calculated in the U.S. As a result, this for example means that only income above $500,001 is taxed at 37%. Income of lower...
Write a Python program that computes the income tax for an individual. The program should ask...
Write a Python program that computes the income tax for an individual. The program should ask the user to enter the total taxable income for the year. The program then uses the tax bracket (as shown below) to calculate the tax amount. This is based on a progressive income tax system which is how income tax is calculated in the U.S. As a result, this for example means that only income above $500,001 is taxed at 37%. Income of lower...
Using the MARIE computer assembly language, write a program that computes the following expression: z =...
Using the MARIE computer assembly language, write a program that computes the following expression: z = a * b * c. The computer will read in the input values a, b, and c from the keyboard and the final result (z) have to be displayed. In addition, every time an input value is read in, it must be displayed on the screen. Remember that the instruction set does not have an instruction to execute multiplication. Note: If any of the...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Write a program that reads in the radius and length of a cylinder and computes volume...
Write a program that reads in the radius and length of a cylinder and computes volume using the following formulas: area = radius * radius * PI volume = area * length
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT