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);



    }

}
peter_budo commented: Writing in captital letters is rude, it is like shouting on the other person -4
Ezzaral commented: And shouting "AS SOON AS POSSIBLE" is even worse. -3

Recommended Answers

All 5 Replies

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

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

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??

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

edensigauke: please check the dates. you are replying to a post that is a year old.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.