2

I'm looking to generate an M-Arithmetic triangle of order 4. It's described here: http://www.sciencedirect.com/science/article/pii/S0024379509003577?np=y

And it looks like this:

1
1 1 1 1 
1 2 3 4 3 2 1
1 3 6 10 12 12 10 6 3 1 
1 4 10 20 31 40 44 40 31 20 10 4 1

And so on. The first two rows are always constant in my M-arithmetic triangle of order 4. From then on, each term is the sum of the term above it and the 3 terms on the left of the term above it.

The variables to define the size of the matrix which will contain these numbers are as follows

int y = user.nextInt();
int max = 3*y + 1;
int[][] m-arith = new int [y][max];

How do I generate the M-arithmetic triangle in code? As well as put zeroes into all the places not filled by numbers? I could declare the matrix manually like this (just gonna show a few rows):

int[][] m-arith = { 
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {1,2,3,4,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                      
    };

But this seems like a monumental waste of time if I need anything more than a few rows.

Any ideas?

edit: For that matter, the code to generate an M-arithmetic triangle of any order would be interesting. However, am looking for solution specific to the order 4 triangle.

Thev
  • 1,105
  • 2
  • 13
  • 24

1 Answers1

0

First, the default value of an integer in Java is zero. you don't have to initialise those.

Code:

int y = user.nextInt();
int max = y*(y-1) + 1; // <--- updated here, change 3 with y-1
int[][] mat = new int [y+1][max]; // y+1 not y (for degree 4 we have 5 rows)

// ask user to enter the first two constant rows
mat [0][0] = user.nextInt();
for (int i=0;i<y;i++)
    mat [1][i] = user.nextInt();

for (int i=2;i<y+1;i++)
{
    for (int j=0;j<(y-1)*i+1;j++)
    {
        for (int k=j;k>=j-y+1;k--)
        {
            mat[i][j] += mat[i-1][k];
        }
    }
}

// print your array here!

Dynamic degree and dynamic constants

Note: as some pointed. 2D arrays in Java can be triangular.

hasan
  • 23,815
  • 10
  • 63
  • 101
  • 1
    You could also initialise each row to the correct size `i*3+1` rather than `max`. – biziclop Aug 06 '15 at 09:47
  • sure but not i*3+1. its (y-1)*i+1 – hasan Aug 06 '15 at 09:51
  • @hasan83 This solution works, but it depends upon the users entering the integers. Also, the number of rows should be y. I'm more looking to generate this entire thing in a similar manner to how you would a Pascal's triangle. – Thev Aug 06 '15 at 10:00
  • what do you mea it depends upon user entering the data? so what? its a dynamic degree solution. for sure the user will enter the data! for degree 4 which the logical thing to ask the user to enter as a value for y, the rows number must be/ should be 5 (y+1). – hasan Aug 06 '15 at 10:07
  • this solution exactly produces your output and solves the problem described in your question. nothing in the problem related to pascal's triangle. pascal's not mentioned in your question and doesn't affect it. – hasan Aug 06 '15 at 10:08
  • if you want you can ask another question about pascal's?!! – hasan Aug 06 '15 at 10:09
  • When the degree is 4 as described by your question we should have 5 rows. is that right? – hasan Aug 06 '15 at 10:45
  • @hasan83 Yup, I apologise. You're right about the number of rows being y + 1. The problems with having the user enter the values is that this code is to be implemented in an app. The user can't be expected to fill in parts of the code for me. – Thev Aug 07 '15 at 00:51
  • @hasan83 I wasn't looking to generate Pascal's triangle, but in the same way that the Pascal's triangle can be generated programmatically, I was wondering if M-arithmetic triangles could be generated in the same way. – Thev Aug 07 '15 at 00:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85359/discussion-between-thevesh-theva-and-hasan83). – Thev Aug 07 '15 at 00:57
  • Looks like it is where us the up vote and accepted answer. I dont see any. – hasan Aug 07 '15 at 08:41
  • Looks like that you can generate it – hasan Aug 07 '15 at 08:45