Answers
Algorithm 1
//We will adopt the standard algorithm writing
//conventions as per Donald Knuth's book, The Art of Computer Programming
-------------
Answer to 1)
--------------
algorithm find_area
input:
shaded_box_length, shaded_box_width, white_box_length, white_box_width
Integers shaded_box_x, shaded_box_y, white_box_x, white_box_y: shaded_box_x is the number of shaded boxes in horizontal direction, white_box_y is the number of white boxes in verticial direction. Other definitions follow similarly.
output:
area: the area of shaded boxes in total
number_of_shaded_boxes <- shaded_box_x * shaded_box_y
number_of_white_boxes <- white_box_x * white_box_y
area_of_shaded_box <- shaded_box_length * shaded_box_width
area_of_white_box <- white_box_length * white_box_width
area <- (number_of_shaded_boxes * area_of_shaded_box) - (number_of_white_boxes * area_of_white_box)
if area < 0
print 'Area cannot be negative'
else
print(area)
Explanation:
The algorithm simply calculates the area of shaded region by subtracting the total area of bigger box from the white boxes. The boundary conditions for values input to the program are not checked based on the assumption that a input function is doing the constraint checking. It should check for positive real values for dimensions entered and the dimensions of white box should not exceed the outer shaded box dimensions.
------------
Answer to 2)
------------
algorithm calculate_interest
input:
interest_rate: the rate of interest
principal: original loan amount
payment: payment made in this year
amount: outstanding amount before the start of the year
year: current year number in which payments were made. eg.
2017 or 2018.
year_of_loan: year in which loan was taken
output:
interest: total interest on the loan in this year
balance: outstanding amount at the end of this year
balance <- amount - payment
interest <- interest_rate*balance/100
amount <- balance + interest //amount for next year
Explanation:
The algorithm takes in the the remaining balance prior to this year and then calculates the balance that will remain at the end of this year.
Interest is calculated using the formula I = PRT/100 where P is the principal amount, R is rate of interest and T is time. Here, T is 1 since we are calculating for each year.
------------
Answer to 3)
------------
algorithm medical_payments
input:
total_charges: total charges overdue
prescription_charge: charge per prescription
prescription_quantitiy: number of prescriptions
output:
customer_pay: amount paid by the patient
insurance_company_pay: amount paid by the insurance company
assumption:
presciption charges are excluded from total charges
variables:
customer_deductible_amount: It is set to $1000 amount that each customer pays in the beginning.
assumption: total charges is greater than the deductible amount
co_pay: fixed amount to be paid by the customer
prescription_pay: total prescription charge
prescription_co_pay: co pay for each prescription by customer
prescription_insurance_pay: amount paid by insurance company for the presciption
if customer_deductible_amount > total_charges
customer_deductible_amount <- customer_deductible_amount - total charges
co_pay <- total_charges
insurance_company_pay <- 0
else
co_pay <- 0.2 * (total_charges - customer_deductible_amount)
insurance_company_pay <- total_charges - co_pay
prescription_pay <- prescription_quantity * prescription_charge
prescription_co_pay <- prescription_quantity * 40
prescription_insurance_pay <- prescription_pay - prescription_co_pay
customer_pay <- co_pay + prescription_co_pay
insurance_company_pay <- insurance_company_pay + prescription_insurance_pay
Explanation:
The algorithm checks if the total charge is less than the deductible amount that customer has. If it is true than the amount is deducted from the deductible and insurance company doesn't pay anything other than the prescription fees. Otherwise, the 20% of the amount is paid by the customer and rest by insurance company. For presciptions, $40 is paid per prescription by the customer and rest by the insurance company.
Prescription charges are not included in the deductible and insurance company pays for them.
.