1. ## Problem with Matrices

Hello all,

I'm quite new to programming, however I do have an assignment I've to do. Now the assignment itself is using the Jacobi iteration method to solve for 15 unknowns. I figured the best way to do it would be using matrices, as that kinda seemed simpler. Now, the problem is that, if A is a matrix that is the sum of U, L and D, where U and L are the lower and upper triangular parts of the matrix, and D is the diagonal part of the matrix, how do I separate U and L from the rest?

I got the diagonal of the matrix fine, but when I try to run a loop for separating U and L, it just doesn't give the correct answers. I know this is probably a very badly written loop, so I apologize for the 'newbiness' of it beforehand

P.S: It DOES give the correct answer for the Lower part, just not the upper part.
P.P.S: I didn't mention that the matrices would all be square matrices, as to solve for linear equations you'd need to have that and such.. But hey just in case someone asks.

By the way, I'm right now trying the loop for 2x2 matrix, then I'll move on to the larger ones.

Code:
```for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
k = i;

if (i == j)
{
if (i != 0)
{

while (k > 0)
{
k--;
U[k][j] = A[k][j];
}
}

k = j;

if (i != n-1)
{
while (k < n-1)
{
k++;
L[k][j] = A[k][j];
}
}
}
}
}```

2. You can't write up code until you know the logic you need to solve the problem.

So study the problem - show us your logic with pseudo code, working on an example problem. That's where you should be working, imo.

It's great that you have part of the problem figured out, but that's not enough. (clearly, or you wouldn't be here).

If someone has to tell you how to solve the problem, and then someone has to help you code up the program, then WHAT is it that YOU do?

Don't take this personally, because I don't mean it that way. It's the way you're approaching this assignment, that I don't like. It's not productive, in my opinion, and it doesn't work well as an educational methodology either.

3. I apologize if I gave the impression that I was asking for someone else to do my assignment for me, which I sincerely did not. I do have the code itself, however, as the loop I previously mentioned doesn't work, neither does the algorithm itself. I know that the code is pretty ugly in itself, and I do apologize for it. Once I get the algorithm working, I will split it into a few functions (e.g a function for matrix multiplication, as it occurs way too often in the main for now).

As said, right now the code is more or less a draft of what it should be.

P.S.: I am using an example from wikipedia which I have checked for errors using other methods and found that it is a correct example/solution.
P.P.S.: I've already worked the problem out on paper, but for some reason my loops don't exactly work as I imagined the would

HERE is the link for it.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
int n = 2;
int i, j, k, l;
float A[n][n], B[n], C[n], D[n][n], Dinv[n][n], L[n][n], R[n][n], T[n][n], U[n][n], Y[n];
float X[100], error = 1e-5;
int x0 = 1;

//Every member of the arrays initialized to zero

for (i = 0; i <= n-1; i++)
{
for (j = 0; j <= n-1; j++)
{
A[i][j] = 0.0;
D[i][j] = 0.0;
Dinv[i][j] = 0.0;
L[i][j] = 0.0;
R[i][j] = 0.0;
T[i][j] = 0.0;
U[i][j] = 0.0;
}
}

for (i = 0; i <= n-1; i++)
{
B[i] = 0;
C[i] = 0;
Y[i] = 0;
}

X[0] = x0;
X[1] = x0;

A[0][0] = 2;
A[0][1] = 1;
A[1][0] = 5;
A[1][1] = 7;
B[0] = 11;
B[1] = 13;

for (i = 0; i < n; i++)
{
D[i][i] = A[i][i];
Dinv[i][i] = 1/D[i][i];
}

for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
k = i;

if (i == j)
{
if (i != 0)
{

while (k > 0)
{
k--;
U[k][j] = A[k][j];
}
}

k = i;

if (i != n-1)
{
while (k < n-1)
{
k++;
L[k][j] = A[k][j];
}
}
}
}
}

for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
R[i][j] = L[i][j] + U[i][j];
}

for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
for (k = 0; k < n; k++)
T[i][j] += -Dinv[i][k] * R[k][j];
}
}

for (i = 0; i < n; i++)
{
for (j = 0; j < 1; j++)
{
for (k = 0; k < n; k++)
{
C[i] += Dinv[i][k] * B[k];
}
}
}

i = 0;
int z = 0;

do
{
z = 0;

for (l = 0; l < n; l++)
{
for (j = 0; j < 1; j++)
{
for (k = 0; k < n; k++)
{
Y[l] += T[l][k] * X[k];

}
}

}

X[i+2] = Y[z] + C[z];
X[i+3] = Y[z+1] + C[z+1];

printf("%f\n%f", X[i+2], X[i+3]);
i++;

} while (i < 3);

return 0;
}```