Note that there are some explanatory texts on larger screens.

plurals
  1. POLoop issue, stop prematurely
    primarykey
    data
    text
    <p>I have a homework assignment which I feel I am close to getting right. The assignment is as follows:</p> <p>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.</p> <p><img src="https://lh5.googleusercontent.com/-cgufNsx0CWo/Tq7tw6DHn1I/AAAAAAAAABA/kgs1SjSZ4Q8/s144/scan035.jpg" alt="diagram"></p> <p>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.</p> <p>Write a program to find all unsafe banks. Your program reads the input as follows. It first reads two integers, <code>n</code> and <code>limit</code>, where <code>n</code> indicates the number of banks and <code>limit</code> is the minimum assets for keeping a bank safe. It then reads <code>n</code> lines that describe the information for <code>n</code> 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)</p> <blockquote> <p>5 201</p> <p>25 2 1 100.5 4 320.5</p> <p>125 2 2 40 3 85</p> <p>175 2 0 125 3 75</p> <p>75 1 0 125</p> <p>181 1 2 125</p> </blockquote> <p>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"</p> <p>This is my current solution to the problem. I can't figure out how to get it to find all the unsafe banks. Just the first one. I have it set to take static input for testing. I have the working user input code ready to go if I can just get it to work properly.</p> <pre><code>#include &lt;iostream&gt; 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 &lt;&lt; "Enter number of banks and the limit:" &lt;&lt; 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 &lt; nobanks; i++) { isSafe[i] = true ; } cout &lt;&lt; "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 &lt; nobanks; j++) // Check if a bank has loans and add them to assets { if (loan[i][j] &gt;= 0) assets[i] += loan[i][j]; } if (assets[i] &lt; limit) // Check to see if current bank meets limit { isSafe[i] = false; // Set bank to not safe if limit not met newunsafefound = true; cout &lt;&lt; i &lt;&lt; " " ; //Display the bank that is unsafe and a space for the next bank k=0; for (k = 0; k &lt; nobanks; k++) { loan[i][k] = 0; //Set banks loans to 0 if unsafe. k++; } } i++; } while(i &lt; nobanks); } return (0); } </code></pre> <p>What am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload