title: Variable's value change affects my entire output in a Spiral Matrix Program
body: <p>Below is the working code:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class spiralMatrix1 {
  
  public static void main(String args[]) {
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of columns and rows in the array");
      int c = in.nextInt();
      int r = in.nextInt(), arr[][] = new int[c][r];
      int k = 1, c1 = 0, c2 = c - 1, r1 = 0, r2 = r - 1, i, j, a = 0;    //i for c and j for r.
      while (k &lt;= r * c) {
          for (i = c1; i &lt;= c2; i++) {
              arr[r1][i] = k++;
          }
          for (j = r1 + 1; j &lt;= r2; j++) {
              arr[j][c2] = k++;
          }
          for (i = c2 - 1; i &gt;= c1; i--) {
              arr[r2][i] = k++;
          }
          for (j = r2 - 1; j &gt;= r1 + 1; j--) {
              arr[j][c1] = k++;
          }
          c1++;
          c2--;
          r1++;
          r2--;
      }
      System.out.println("The Spiral array Is:");
      for (i = 0; i &lt;= c-1; i++) {
          for (j = 0; j &lt;=r-1; j++) {
              System.out.print(arr[i][j] +" ");
          }
          System.out.println();
      }
      System.gc();
      Runtime.getRuntime().gc();
  } }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>Enter the number of columns and rows in the array
  5
  5
  The Spiral array Is:
  1 2 3 4 5 
  16 17 18 19 6 
  15 24 25 20 7 
  14 23 22 21 8 
  13 12 11 10 9 
  </code></pre>
  
  <p>Now if in the above code I change the value of k from 1 to 0 (or better, when I change the beginning value of k in the while loop to be 0 rather than 1 as above), the program gives a ArrayIndexOutOfBounds Exception. The program and the output is given below:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class spiralMatrix1 {
      public static void main(String args[]) {
          Scanner in = new Scanner(System.in);
          System.out.println("Enter the number of columns and rows in the array");
          int c = in.nextInt();
         int r = in.nextInt(), arr[][] = new int[c][r];
          int k = 0, c1 = 0, c2 = c - 1, r1 = 0, r2 = r - 1, i, j, a = 0;    //i for c and j for r.
      while (k &lt;= r * c) {
          for (i = c1; i &lt;= c2; i++) {
              arr[r1][i] = k++;
          }
          for (j = r1 + 1; j &lt;= r2; j++) {
              arr[j][c2] = k++;
          }
          for (i = c2 - 1; i &gt;= c1; i--) {
              arr[r2][i] = k++;
          }
          for (j = r2 - 1; j &gt;= r1 + 1; j--) {
              arr[j][c1] = k++;
          }
          c1++;
          c2--;
          r1++;
          r2--;
      }
      System.out.println("The Spiral array Is:");
      for (i = 0; i &lt;= c-1; i++) {
          for (j = 0; j &lt;=r-1; j++) {
              System.out.print(arr[i][j] +" ");
          }
          System.out.println();
      }
      System.gc();
      Runtime.getRuntime().gc();
  }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Enter the number of columns and rows in the array
  5
  5
      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2147483648
  at spiralMatrix1.main(spiralMatrix1.java:19)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  </code></pre>
  
  <p>This program was created using IntellijIDEA IDE and once the above output was generated. </p>