MATLAB You were just hired by a bank in the area to fix a buggy program written
ID: 3803696 • Letter: M
Question
MATLAB
You were just hired by a bank in the area to fix a buggy program written by a since fired programmer. This function is supposed to do the following:
*Receive a method (withdraw, deposit, or balance)
*Perform the respective operation and return the current account balance
*If the withdrawal amount exceeds the balance, cancel the transaction and return the balance
The account balance should persist between function calls. For example, if you did the following:
The main function header, bankingsystem() has been written for you and it is up to you to fix the issues so this bank can be up and running again!
function balance = bankingsystem(mode, amount)
Explanation / Answer
script
prompt='Enter d for deposit w for withdrawal or b for checking balance....Initially press r for reset ';
mode=input(prompt,'s');
prompt='Enter the balance';
bal=input(prompt);
balance=bankingsystem(mode,bal);
promt='Remaining balance =';
balance
bankingbalance function
function [balance]=bankingsystem(mode,bal)
persistent balan; //persistent defines variables that are local to the function but their values are retained in memory between function calls
if strcmp(mode,'r') // for initializing the persistent variable
balan=0;
balance=balan;
else
if strcmp(mode,'d')
balance=balan+bal;
balan=balance;
elseif strcmp(mode,'w')
balance=balan-bal;
balan=balance;
else
balance=balan;
end
end
end
output
>> bank
Enter d for deposit w for withdrawal or b for checking balance....Initially press r for reset r
Enter the balance 0
balance =
0
>> bank
Enter d for deposit w for withdrawal or b for checking balance....Initially press r for reset d
Enter the balance 10000
balance =
10000
>> bank
Enter d for deposit w for withdrawal or b for checking balance....Initially press r for reset w
Enter the balance 500
balance =
9500
>> bank
Enter d for deposit w for withdrawal or b for checking balance....Initially press r for reset b
Enter the balance 0
balance =
9500
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.