dato000 - 1 year ago 39

C++ Question

I need your help.

I got this interesting problem, looks more like homework, but it is really complicated.

I need to fill the elements in this array of numbers where, given certain list of numbers, I need to put them in the diagonals of the array, and then, fill the rest of positions with the sumatory of the initial numbers, I think I could explain way better if I give an example:

Numbers of the array: 7 6 5 4 3 2 1 -1

The Array is fill with these numbers: 7 6 5 4 3 2 1

La array[n][m] is:

**7** 13 18 22 25 27 28

13 **6** 11 15 18 20 21

18 11 **5** 9 12 14 15

22 15 9 **4** 7 9 10

25 18 12 7 **3** 5 6

27 20 14 9 5 **2** 3

28 21 15 10 6 3 **1**

I tried a long of ways to do it, in c++:

`//Here is the simple thing, setting the diagonals of the array[m][n]`

for(int row=0; row < maxNumber; fila++){

for(int column=0; column < maxNumber; column++)

{

if(row == column)

{

matAuto[row][column] = elements[row];

}

}

}

I was trying to make a temporary array with the elements of the array, and in the loop, sum the numbers according with their positions, you know like bubble sort (array[m][n] = listNum[n] + listNum[n+1]

`if(row < column)`

{

matAuto[row][column] = elements[row] + elements[row+1];

}

if(row < column)

{

matAuto[row][column] = elements[column] + elements[column+1];

}

if(row < column)

{

matAuto[row][column] = elements[column] + elements[row+1];

}

if(row < column)

{

matAuto[row][column] = matAuto[row][column] + matAuto[row+1][column+1];

}

And so...nothing worth it.

I don't know if is right to add the complete code, is not long, but well, Is just the common C++ stuff, but I got it in spanish, hope it doesn't botter you guys.

Answer Source

As @Makketronix mentioned, you can use the fact that your two triangular matrices are symmetrical.

```
for(int row=0; row < maxNumber; row++){
for(int column=row; column < maxNumber; column++)
{
if(row == column)
{
matAuto[row][column] = elements[row];
}
else
{
int newSum = matAuto[row][column-1] + elements[column];
// assign with mirror
matAuto[row][column] = newSum;
matAuto[column][row] = newSum;
}
}
}
```

Here's the result: