This site requires JavaScript, please enable it in your browser!
Greenfoot back
Mux
Mux wrote ...

2013/2/3

How to programm pascal's triangle??

Mux Mux

2013/2/3

#
I'm pretty sure, that the basic stuff is right:
public float[] getBinomKoef(int maxLength)
    {
        float[] koef = new float[2500];
        float[] koef2 = new float[2500];
        int nowLength = 1;
        koef[0]=1;
        int nStore = 0;
        while( maxLength > nowLength)
        {
            koef2 = koef;
            for(int n = 1; n < nowLength; n++)
            {
                koef[n] = (koef2[n-1])+(koef2[n]);
                nStore = n;
            }
            koef[nStore+1] = 1;
            nowLength++;
        }
        return koef;
    }
in the koef vector should be a list of the certain stage(maxLength) of the triangle. But the output is just crap and has no structure. I can't figure out whats wrong. It would be nice to get a tip, where the problem could be.
danpost danpost

2013/2/4

#
I used 'ints', but you can change them back. I think this is what you wanted.
private int[] getKoef(int exponent)
{
    int[] koef2= new int[1];
    koef2[0]=1; // sets the first line
    for(int x=0; x<exponent; x++)
    { // for each additional line
        int[] koef=new int[koef2.length+1]; // increase size of line
        koef[0]=1; // sets the first coefficient
        koef[koef.length-1]=1; // sets the last coefficient
        for(int i=1; i<koef.length-1; i++) koef[i]=koef2[i-1]+koef2[i]; // sets the rest
        koef2=koef;
    }
    return koef2;
}
Mux Mux

2013/2/4

#
It's exactly what i was looking for. thx
You need to login to post a reply.