Financial Tsunami Banks lend money to each other. In tough economic times, if a
ID: 3640550 • Letter: F
Question
Financial Tsunami
Banks lend money to each other. In tough economic times, if a bank goes bankrupt, it may not be able to pay back the loan (You might remember 2008 banking crisis). A bank’s total assets are its current balance plus its loans to other banks. Figure 1 is a diagram that shows five banks. 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. Your program reads the input as follows. It first reads two integers n and limit, where n indicates the number of banks and limit the minimum total assets for keeping a bank safe. It then reads n lines that describe the information for n banks with id from 0 to n-1. The first number in the line is the bank’s balance, the second number indicates the number of banks that borrowed money from the bank, 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 is as follows (note that the limit is 201):
>5 201
>25 2 1 100.5 4 320.5
>125 2 2 40 3 85
>175 2 0 125 3 75
>75 1 0 125
>181 1 2 125
The total assets of bank 3 are (75 + 125 = balance + loan), which is under 201. So bank 3 is unsafe. After bank 3 becomes unsafe, the total assets of bank 1 will fall below (125 + 40). So, bank 1 is also unsafe. The output of the program should be
>Unsafe banks are 3 1 (Hint: Use a two-dimensional array borrowers to represent loans. borrowers[i][j] indicates the loan that bank i loans to bank j. Once bank j becomes unsafe, borrowers[i][j] should be set to 0.)
Explanation / Answer
please rate - thanks
I left debugging code as comments
import java.util.*;
public class main
{public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n,limit,i,m,num,j;
boolean unsafe=true;
System.out.print("Enter number of banks and saftey limit: ");
n=in.nextInt();
limit=in.nextInt();
double [][]bank=new double[n][n];
double[]balance=new double[n];
double[]starting=new double[n];
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
bank[i][j]=0;
starting[i]=0;
}
System.out.println("for each bank enter the bank’s balance");
System.out.println("followed by the number of banks that borrowed money from the bank");
System.out.println("followed by pairs of two numbers. Each pair describes a borrower.");
System.out.println("The first number in the pair is the borrower’s id and the second is the amount borrowed");
for(i=0;i<n;i++)
{System.out.print("For bank "+(i+1)+": ");
starting[i]=in.nextDouble();
m=in.nextInt();
for(j=0;j<m;j++)
{num=in.nextInt();
bank[i][num]=in.nextDouble();
}
}
//printbank(bank,balance,n);
//check balance after borrowed
while(unsafe)
{unsafe=false;
for(i=0;i<n;i++)
{if(starting[i]>0)
{balance[i]=starting[i];
for(j=0;j<n;j++)
balance[i]+=bank[i][j];
if(balance[i]<limit)
{unsafe=true;
starting[i]=0;
for(j=0;j<n;j++)
bank[j][i]=0;
}
// System.out.println("Bank "+i+" "+balance[i]);
}
// printbank(bank,balance,n);
}
}
//printbank(bank,balance,n);
System.out.println("The unsafe banks are ");
for(i=0;i<n;i++)
if(balance[i]<limit)
System.out.print(i+" ");
}
/*public static void printbank(double bank[][],double balance[],int n)
{int i,j;
System.out.println();
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
System.out.print(bank[i][j]+" ");
System.out.println(balance[i]);
}
}*/
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.