Note that there are some explanatory texts on larger screens.

plurals
  1. POIndex out of bounds exception in homework
    text
    copied!<p>I'm trying to do a homework assignment. I have to use dynamic programming to display whether the next person to move is in a win/loss state. I don't need help with the actual problem, I need help with an index out of bounds exception I'm getting that baffles me. I'm only going to paste part of my code here, because I only need the for loops looked at. I also don't want anyone in my class seeing all my code and copying it. If you need more data please let me know. So here is the code:</p> <pre><code>if(primeArray[x] == true){ for(int i = 1; i &lt;= x; i++){ if(primeArray[i]== true){ newRowNumber = x - i; } if(dynaProgram[newRowNumber][columnNumber] &lt; minimum){ minimum = dynaProgram[newRowNumber][columnNumber]; } } } //COMPOSITE CASE FOR X! else{ for(int k = 1; k &lt;= x; k++){ if((primeArray[k] == false)){ newRowNumber = x - k; } if(dynaProgram[newRowNumber][columnNumber] &lt; minimum){ minimum = dynaProgram[newRowNumber][columnNumber]; } } </code></pre> <p>For some reason the <code>if(primeArray[i] == true</code> runs correctly, but I'm getting index out of bounds exception on <code>if(primeArray[k] == false</code>. The only difference between these two is the use of the variable k over i in the for loop.(the for loops are identical) I haven't used either variables anywhere else in my code. I have no idea why this occurs for one but not the other. In both cases, x remains the same number.</p> <p>I am also getting an index out of bounds exception on the second <code>minimum = dynaProgram[newRowNumber][columnNumber]</code>, while the first doesn't encounter an error. I know it's probably a stupid error, but I can't figure it out. If I change the 'k' for loop to <code>k &lt; x</code> the index of out bounds exception in the <code>if(primeArray[k] == false</code> line goes away, but then it isn't correct. (The error on the second <code>minimum = dynaProgram[newRowNumber][columnNumber]</code> doesn't go away however.)</p> <p>All this code is in a nested for loop which iterates through the rows and columns in the table to fill them in. If I remove the above code and just put <code>dynaProgram[rowNumber][columnNumber] = 1</code> I don't have an issue, so I don't believe that is the problem.</p>
 

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