-2

hello friends .this code have error-ARRAY OUT OF BOUND
BUT I AM UANBLE TO FIND WHERE IS IT AND HOW TO REMOVE IT.. PLEASE GIV ME SOLUTION AS SOON AS POSSIBE.

//import java.util.Scanner;


import java.lang.System;
 class PascalTri
{


    //public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Scanner sc = new Scanner(System.in);
        //System.out.println("How many rows should the triangle have ? ");
        //int rowsNumber = sc.nextInt();


    //  try{


            static void calculatePascalTriangle (int rowsNumber )

            {
                int a[][] = new int[rowsNumber-1][];
                for(int i=0;i<rowsNumber; i++)
                {
                    a[i]=new int[i];

                }
                if(rowsNumber==1)
                {
                    a[0][0]=1;
                    System.out.println(a[1][1]);
                }
                else if(rowsNumber==2)
                {
                    a[1][0]=1;
                    a[1][1]=1;
                    System.out.println(a[1][1]);
                }
                    else
                    {
                        for(int i=2;i<rowsNumber;i++)
                        {
                            a[i][1]=1;
                            System.out.println(a[i][1]);
                            for(int j=2;j<=rowsNumber;j++)
                            {
                                a[i][j]=a[i-1][j-1]+a[i-1][j];
                                System.out.println(a[i][j]);
                            }

                        }
                    }
            }

        }

//      catch(Exception e )
        //{

        //}
        //}

public class PascalTriangle{

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //PascalTri ob=new PascalTri();
        PascalTri.calculatePascalTriangle(4);



    }

}

Edited by mike_2000_17: Fixed formatting

Comments
And shouting "AS SOON AS POSSIBLE" is even worse.
Writing in captital letters is rude, it is like shouting on the other person
4
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by stultuske
0

We are not mind readers! The message that told you about the array index out of bounds also told you EXACTLY what the invalid index was, and which line of your code it happened in. You must share that info if you want someone to help.
Anyway, this code is a disaster if rowsNumber is 1::

int a[][] = new int[rowsNumber-1][]; // when  rowsNumber is 1,  size is [0][]
for(int i=0;i<rowsNumber; i++)  {
  a[i]=new int[i]; // a[i] is invalid for all values of i if array has zero size
0

look here, you must use a vector and not an array, if you are to use an array then it will always be limited and not dynamic

0

Hello edensigauke,
in this code i am passing rowsNumer(first index of array) and den allacating 2nd index of array .. so i think its a dynamic array. plz tell me if i m bot correct??

0

well Dynamic in your sence of the fact that the number on the array changes, but the fact remains, it doesnt grow.....

Anyway the solution to your dilema is simple

int[][] a = new int[rowsNumber][];

Putting the -1, makes the array 1 less, so it would definitely give you array index out of bounds

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.