PYTHON 3.4 Banks lend money to each other. In tough economic times, if a bank go
ID: 3764314 • Letter: P
Question
PYTHON 3.4
Banks lend money to each other. In tough economic times, if a bank goes bankrupt, it will not be able to pay back
the loan. A bank’s total assets are its current balance plus its loans to other banks. Figure 1 below is a diagram
(a graph) that shows five banks. (The figure is just for illustration purposes). The banks’ current balances are 25,
125, 175, 75, and 181 million dollars, respectively. The directed edge from node 1 to node 2 indicates that bank 1
lends 40 million dollars to bank 2.
If a bank’s total assets are under a certain limit, the bank is unsafe. The money it borrowed cannot be returned to
the lender, and the lender cannot count the loan in its total assets. Consequently, the lender may also be unsafe,
if its total assets are under the limit. Write a program to find all unsafe banks.
201
25 1 100.5 4 320.5
125 2 40 3 85
175 0 125 3 75
75 0 125
181 2 125
The above is LIMIT. that is, the minimum total assets for keeping a bank safe. Each of
the remaining lines contains info about one bank. Eg. line 1 contains info about bank with id 0, line 2
about bank with id 1, line 3 about bank with id 2 ... etc. Thus the bank id-s are from 0 to n-1, where n is the total
number of banks (thus the total number of banks is the number of lines in the file minus 1). The first number in the line is the bank’s balance, and the rest are pairs of two numbers. Each pair describes a borrower. The first
number in the pair is the borrower’s id and the second is the amount borrowed.
For example, the input for the five banks in Figure 1 above, is as follows (note that the limit is 201):
201
25 1 100.5 4 320.5
125 2 40 3 85
175 0 125 3 75
75 0 125
181 2 125
For example, for the above input, the total assets of banks are:
Bank 0: Current assets: 446.0
Bank 1: Current assets: 250.0
Bank 2: Current assets: 375.0
Bank 3: Current assets: 200.0
Bank 4: Current assets: 306.0
The bank 3 has assets 75 + 125= 200, which is under 201. So bank 3 is unsafe. After bank 3 is determined unsafe,
the total assets of the remaining banks change to:
Bank 0: Current assets: 446.0
Bank 1: Current assets: 165.0
Bank 2: Current assets: 300.0
Bank 4: Current assets: 306.0
Thus the assents of bank 1 change to 125 + 40=160 and consequently fall below the limit. Thus bank 1 is also
unsafe. After bank 1 is determined unsafe, the total assets of the remaining banks change to:
Bank 0 Current assets: 345.5
Bank 2 Current assets: 300.0
Bank 4 Current assets: 306.0
None of the remaining banks has assets smaller than the limit, thus we can stop and conclude that the unsafe
banks are 1 and 3 and safe banks are 0,2 and 4.
(Hint: When you find an unsafe bank, say a bank with id i, you should go through the 2D lists banks and for every
bank (i.e. every sublist) that has entry i followed by a number x, you should replace x by zero. Here x represents
3
the number of millions of dollars that that bank lent to the bank i, as explained earlier. And then recompute the
assets of the banks.)
Every time you find one unsafe bank, your program should update the assets of the remaining banks and print
their current assets. To understand what exactly is required of your program and what it must display study
the two example runs below.
Example.
>>>
[[25.0, 1, 100.5, 4, 320.5], [125.0, 2, 40.0, 3, 85.0], [175.0, 0, 125.0, 3, 75.0], [75.0, 0, 125.0],
[181.0, 2, 125.0]]
Safe limit is: 201.0 million dollars
Current unsafe banks are:
Current assets of the banks which are not yet in unsafe list:
Bank 0 Current assets: 446.0 millions
Bank 1 Current assets: 250.0 millions
Bank 2 Current assets: 375.0 millions
Bank 3 Current assets: 200.0 millions
Bank 4 Current assets: 306.0 millions
Adding bank 3 to the list of unsafe banks.
Current unsafe banks are: 3
Current assets of the banks which are not yet in unsafe list:
Bank 0 Current assets: 446.0 millions
Bank 1 Current assets: 165.0 millions
Bank 2 Current assets: 300.0 millions
Bank 4 Current assets: 306.0 millions
Adding bank 1 to the list of unsafe banks.
Current unsafe banks are: 3 1
Current assets of the banks which are not yet in unsafe list:
Bank 0 Current assets: 345.5 millions
Bank 2 Current assets: 300.0 millions
Bank 4 Current assets: 306.0 millions
Unsafe banks are: 1 3
Safe banks are 0 2 4
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 100;
double balance[SIZE];
double loan[SIZE][SIZE];
int nobanks;
int limit;
int i = 0;
int j = 0;
int k = 0;
int noborrowers;
double assets[SIZE];
bool isSafe[SIZE];
bool newunsafefound = true;
cout << "Enter number of banks and the limit:" << endl;
// Set all of the data
nobanks = 5;
limit = 201;
balance[0] = 25.0;
balance[1] = 125.0;
balance[2] = 175.0;
balance[3] = 75.0;
balance[4] = 181.0;
loan[0][1] = 100.5;
loan[0][4] = 320.5;
loan[1][2] = 40.0;
loan[1][3] = 85.0;
loan[2][0] = 125.0;
loan[2][3] = 75.0;
loan[3][0] = 125.0;
loan[4][2] = 125.0;
// Set array to all true values
for(i = 0; i < nobanks; i++)
{
isSafe[i] = true ;
}
cout << "Unsafe banks are: ";
i=0;
while(isSafe[i] == true)
{
newunsafefound=false;
i=0;
do
{
assets[i] = balance[i]; //Set assets to balance
for (j = 0; j < nobanks; j++) // Check if a bank has loans and add them to assets
{
if (loan[i][j] >= 0)
assets[i] += loan[i][j];
}
if (assets[i] < limit) // Check to see if current bank meets limit
{
isSafe[i] = false; // Set bank to not safe if limit not met
newunsafefound = true;
cout << i << " " ; //Display the bank that is unsafe and a space for the next bank
k=0;
for (k = 0; k < nobanks; k++)
{
loan[i][k] = 0; //Set banks loans to 0 if unsafe.
k++;
}
}
i++;
} while(i < nobanks);
}
return (0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.