Docsity

Robust Algorithm-Advanced Unified Engineering-Assignment Solution, Exercises of Aeronautical Engineering

Toggle sidebar
bg1
Home Work 11
The problems in this problem set cover lectures C13 and C14
1.
a. Define a robust algorithm to carry out integer division using repeated subtraction.
Your algorithm accepts two integers and returns the quotient and the remainder.
Hint: What are the preconditions and postconditions of your algorithm?
Precondtions: Two integers x,y
y is non-zero
Algorithm:
Set R to absolute_value(x)
Set Q to zero
While R >= absolute_value (y)
Increment Q
R := R- absolute_value(y)
If either x or y are negative
If both x and y are negative
Set R to –R
else
if x is negative
Set R to –R
Set Q to –Q
Display Q and R
Postconditions: Q contains the quotient
R contains the remainder
x = Q*y + R, abs(R) < abs(Q)
docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Robust Algorithm-Advanced Unified Engineering-Assignment Solution and more Exercises Aeronautical Engineering in PDF only on Docsity!

Home Work 11

The problems in this problem set cover lectures C13 and C

a. Define a robust algorithm to carry out integer division using repeated subtraction.

Your algorithm accepts two integers and returns the quotient and the remainder.

Hint: What are the preconditions and postconditions of your algorithm?

Precondtions: Two integers x , y

y is non-zero

Algorithm:

Set R to absolute_value( x )

Set Q to zero

While R >= absolute_value ( y )

Increment Q

R := R- absolute_value( y )

If either x or y are negative

If both x and y are negative

Set R to –R

else

if x is negative

Set R to –R

Set Q to –Q

Display Q and R

Postconditions: Q contains the quotient

R contains the remainder

x = Q* y + R, abs(R) < abs(Q)

b. Implement your algorithm as an Ada95 program, using exception handling to

provide robustness.

. -- Procedure to carry out robust division . -- Programmer: Jayakanth Srinivasan . -- Date Last Modified : April 17, . --------------------------------------------------------- . . with Ada.Text_Io; . with Ada.Integer_Text_Io; . use Ada.Text_Io; . use Ada.Integer_Text_Io; . . procedure Robust_Division is . X, . Y, . Q, . R : Integer; . Divide_By_Zero : exception; . . begin . loop . Ada.Text_IO.Skip_Line; . begin . -- get the dividend (X) . Ada.Text_Io.Put("Please Enter the X : "); . Ada.Integer_Text_Io.Get(X); . Ada.Text_Io.Skip_Line; . . -- get the divisor (Y) . Ada.Text_Io.Put("Please Enter the Y : "); . Ada.Integer_Text_Io.Get(Y); . Ada.Text_Io.Skip_Line; . . if Y = 0 then . raise Divide_By_Zero; . end if; . . --set the remainder to absolute value of X . R :=abs(X); . -- set quotient to zero . Q := 0 ; . -- while remainder is greater than absolute value of y . while R >= abs(Y) loop . -- deduct absolute value of y from the remainder . R := R - abs(Y) ; . -- increment the quotient . Q := Q + 1; . end loop; . . --ensure that the sign on the quotient is quotient . if (X<0) or (Y<0) then . if (X<0) and (Y<0) then . -- if both x,y are negative then remainder is negative . R := -1*R;

a. What is the cyclomatic complexity of the code fragment shown below?

loop exit when Flag := True;

if A < 100 and B > 200 then if A > 50 then Sum := Sum +2; else Sum := Sum +1; end if; else if B < 300 then Sum:= Sum -1; else Sum := Sum -2; end if; end if;

end loop;

Hint: Draw the control flow graph

11 Nodes, 14 edges => Cyclomatic complexity = 5.

b. What is the minimum number of test cases needed to test the fragment of code

shown below? Justify your answer.

  1. if A < 100 and B > 200 then
  2. if A > 50 then
  3. Sum := Sum +2;
  4. else
  5. Sum := Sum +1;
  6. end if;
  7. else
  8. if B < 300 then
  9. Sum:= Sum -1;
  10. else
  11. Sum := Sum -2;
  12. end if;
  13. end if;

Test Case A B Line Tested

1 50 < A < 100 B > 200 Sum:=Sum+

2 A <= 50 B > 200 Sum:=Sum+

3 A >=100 B < 300 Sum:=Sum-

4 Any Other combination of A and B Sum:=Sum-