C++ Banks loan money to each other. In tough economic times, If a bank goes bank
ID: 3570471 • Letter: C
Question
C++
Banks loan money to each other. In tough economic times, If a bank goes bankrupt it may not be able to pay back the loan. A bank's total assets is its current balance plus its loans to other banks. Figure 8.1 ( attached image ) is a diagram tat 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 loans 40 mill to bank 2
If a banks total asset is under a certain limit, the bank is considered unsafe. If a 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.
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 is the minimum 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 that borrowed money from the bank, and the rest are pairs of two numbers. Each pair describes a borrower. The first number is the banks id and the second number is how much it borrowed. Assume that the maximum number of banks is 100. For example, the input for the five banks is as follows ( 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 is 75 plus 125 which is under 201 so the bank is unsafe. After bank 3 is unsafe the total assets of bank 1 becomes 125 + 40 and is now also unsafe. The output should be "Unsafe banks are 3 1"
(Hint: Use a two-dimensional array borrowers to represent loans. loan[ i ][ j ] indicates the loan that bank i loans to bank j. Once bank j becomes unsafe, loan[ i ][ j ] should be set to 0.)
Explanation / Answer
Just run the code line by line.. I made it simpler than my previous post. Basically StringTokenizer will help you read input values from console and ask me where you have trouble understanding. This should be more than get you started...just display banks fail.
Program Output :
Enter > Banks Total [space] Banks Limit
> 5 201
Enter > BALANCE [space] TOTAL_BANKS_BORROWED [space] BANK_NAME [space] LOAN_AMOUNT....]
Bank [0] > 25 2 1 100.5 4 320.5
Bank [1] > 125 2 2 40 3 85
Bank [2] > 175 2 0 125 3 75
Bank [3] > 75 1 0 125
Bank [4] > 181 1 2 125
Bank [0] Total ASSETS : 446.0M Banks Loaned [1 4 0 0 0 ]
Bank [1] Total ASSETS : 250.0M Banks Loaned [2 3 0 0 0 ]
Bank [2] Total ASSETS : 375.0M Banks Loaned [0 3 0 0 0 ]
Bank [3] Total ASSETS : 200.0M Banks Loaned [0 0 0 0 0 ]
Bank [4] Total ASSETS : 306.0M Banks Loaned [2 0 0 0 0 ]
public class BankLoan {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
int totalBanks = 0;
double bankLimit = 0;
double[] bankAssets;
int[][] borrowers;
// Splits the string input.
java.util.StringTokenizer token = null;
String input = null;
System.out.println("Enter > Banks Total [space] Banks Limit");
System.out.print(" > ");
input = sc.nextLine();
// Split input into tokens by space;
token = new java.util.StringTokenizer(input, " ");
totalBanks = Integer.parseInt(token.nextToken());
bankLimit = Double.parseDouble(token.nextToken());
bankAssets = new double[totalBanks];
borrowers = new int[totalBanks][totalBanks];
System.out.println();
System.out.println("Enter > BALANCE [space] TOTAL_BANKS_BORROWED [space] BANK_NAME [space] LOAN_AMOUNT....]");
for (int bank = 0; bank < totalBanks; bank++) {
System.out.print("Bank [" + bank + "] > ");
input = sc.nextLine();
token = new java.util.StringTokenizer(input, " ");
bankAssets[bank] = bankAssets[bank]+ Integer.parseInt(token.nextToken());
int totalBorrowed = Integer.parseInt(token.nextToken());
for (int borrowed = 0; borrowed < totalBorrowed; borrowed++) {
// stores borrowed bank i loaned to bank j
borrowers[bank][borrowed] = Integer.parseInt(token.nextToken());
// balance + loan = totalAssets
bankAssets[bank] = bankAssets[bank] + Double.parseDouble(token.nextToken());
}
//System.out.println("Bank ["+bank+"] Assets : " +bankAssets[bank]);
}
System.out.println();
for(int i=0; i<bankAssets.length; i++) {
System.out.print("Bank ["+i+"] Total ASSETS : "+bankAssets[i]+ "M Banks Loaned [");
for(int j=0; j<borrowers.length;j++) {
System.out.print(borrowers[i][j]+ " ");
}
System.out.println("]");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.