In: Computer Science
4 Write an algorithm to settle the following questions:
Accept a starting balance (For example :A bank account starts out with $10,000) Accept an Interest rate (For example: 6 percent per year is 0.005 percent per month).
Accept an amount to withdraw every month (For example: Every month, $500 is withdrawn to meet college expenses.)
After how many years is the account depleted? (You are to loop and find out how many months this will take) (For example: Suppose the values ($10,000, 6 percent, $500) were user selectable, then how many months would it take to deplete the account?)
Are there values for which the algorithm you developed would not terminate? (For example if the interest calculated for each month exceeds the withdrawal every month). If so, change the algorithm to make sure it always terminates.
Don't write the program, just write it in pseudo
English.
The solution of the given scenario with includes the following actions in the pseudocode:
The complete Pseudocode is given as below:
-----------------------------------------------------------------------------------------------------------
START:
CreateAccount:
Enter student details;
Deposit more than $10,000 as total deposit;
Enter monthlywithdrawalamount which the student need to withdraw every month to meet college expenses;
IF(monthlywithdrawalamount > total deposit)
Then: Ask the student to enter new monthly limit as it exceeds the deposited amount
ENDIF;
Totalbalance = DepositAmount+ Monthly Interest; // Interest will be added every month
WithdrawalTotalMonths = (Totalbalance)/ monthlywithdrawalamount
The student can withdraw the money for WithdrawalMonths number of months
WidthdrawAmount:
Set withdrawTotalMonths == 0;//counts the number of months for which the amount is withdrawn
While(MonthstoDeplete >= withdrawTotalMonths )
Withdraw the money for the student
Set withdrawMonths = withdrawMonths + 1
endwhile
IF( totalAmount > 0 and MonthstoDeplete < withdrawTotalMonths)
Withdraw the left amount and set the account balance to 0;
ENDIF;
END;
-------------------------------------------------------------------------------------------------------------------------
Explanation of pseudocode for a better understanding:
CreateAccount code will create a new account of the student. The student will need to add the asked details and make a deposit of $10,000 or more. The monthly withdrawal will be asked from the student in below code:
CreateAccount:
Enter student details;
Deposit more than $10,000 as total deposit;
Enter monthlywithdrawalamount which the student need to withdraw every month to meet college expenses;
Once the monthly withdrawal amount is entered by the student, the same will be validated against the total amount to make sure it is less than the total amount and student will be asked to enter a new amount in below code:
IF(monthlywithdrawalamount > total deposit)
Then: Ask the student to enter new monthly limit as it exceeds the deposited amount
ENDIF;
The below calculations will be made to calculate the total amount left in the account after addition of monthly interest . The left amount in the account will calculate the number of months for which the student can withdraw the money:
Totalbalance = DepositAmount+ Monthly Interest; //interest will be added monthly
WithdrawalTotalMonths: (Totalbalance)/ monthlywithdrawalamount
The student can withdraw the money for WithdrawalMonths number of months
The below code will maintain a variable for counting the number of months for which the student has withdrawn the money and before every withdrawal check, the remaining month to withdraw the money will be compared. If the student has months for which the money can be withdrawn, the amount will be deducted .
Set withdrawMonths == 0;//counts the number of months for which the amount is withdrawn
While(withdrawMonths >= withdrawTotalMonths and Totalbalance >0)
Withdraw the money for the student
Set withdrawMonths = withdrawMonths + 1
Endwhile
If the amount left in the account if less than the monthly limit and the withdrawal months has been completed, below code will withdraw the remaining amount and delete the account:
IF( totalAmount > 0 and MonthstoDeplete < withdrawTotalMonths)
Withdraw the left amount and set the account balance to 0;
ENDIF;