# Thread: how to compile the blue part?

1. ## how to compile the blue part?

prob2(): By using a pointer to pointer **A and the function malloc()
allocate the memory for the 4x4 matrix A[][]. By using a pointer *b
and the function malloc() allocate the memory for the 4-dimensional vector
b[]. Read the components of A and b from the given input file matrix.dat.
The last line of the input file contains the components of b. The rows of
matrix A are the first four lines of the input file. Print the components
of A[][] row by row, and the components of b[]. Using the function gauss()
given in the lecture notes week9.txt, solve the system of linear algebraic
equations A[][]*x[]=b[]. Print the components of the solution vector x[].
Calculate and print the length of this vector. Free the allocated memory.
..

the code that i constructed is as followed starting with the gauss
Code:
double **A, *b;
int row;
s=gauss(4,A,b);
printf("x[]={%7.3f", s);

for(row=0; row<4; row++) free(A[row]);
printf("\nThe length of x[] is");
free(A);
free(b);
fclose(input);
}
return;
}

double gauss(int n, double **A, double *b)
{
double one = 1.0, zero=0.0;
double i, j;
double s;
int nm, row, col, Arow;
nm = n-1;
if(n==1)
{
b[0] /= A[0][0]; A[0][0] = one;
return;
}
for(row=0; row<nm; row++)
{
i = A[row][row]; A[row][row] = one;
for(col=row+1; col<n; col++) A[row][col] /=i;
b[row] /=i;
for(Arow=row+1; Arow<n; Arow++)
{
j=A[Arow][row]; A[Arow][row]=zero;
for(col=row+1; col<n; col++) A[Arow][col] -= j * A[row][col];
b[Arow] -= j * b[row];
}
}
b[nm] /= A[nm][nm];
for(row=nm-1; row>=0; row--)
{
for(col=row+1; col<n; col++) b[row] -=A[row][col]*b[col];
s=b[row];
}
return s;

2. Whata do you mean? What's the problem?

3. I tried to printf x[];
but the result gave me nothing! is there something wrong with "double gauss(int n, double **A, double *b)?

4. You never allocate any memory for A and b, only free it.

Also the code is a bit fragmented, and you should use a [code] [/code] box.

5. Meh, added tags, but the indentation is still crap.
SourceForge.net: Indentation - cpwiki

6. i still dont understand...if you were to write it? how would you structure it? since it uses fscanf/sscanf...im confused

7. Perhaps if the code was indented half-way decent,
a) you might figure out the answer

Code:
double gauss(int n, double **A, double *b)
{
double one = 1.0, zero=0.0;
double i, j;
double s;
int nm, row, col, Arow;

nm = n-1;

if(n==1)
{
b[0] /= A[0][0];
A[0][0] = one;
return;
}

for(row=0; row<nm; row++)
{
i = A[row][row];
A[row][row] = one;
for(col=row+1; col<n; col++)
A[row][col] /=i;
b[row] /=i;

for(Arow=row+1; Arow<n; Arow++)
{
j=A[Arow][row];
A[Arow][row]=zero;
for(col=row+1; col<n; col++)
A[Arow][col] -= j * A[row][col];
b[Arow] -= j * b[row];
}
}

b[nm] /= A[nm][nm];
for(row=nm-1; row>=0; row--)
{
for(col=row+1; col<n; col++)
b[row] -=A[row][col]*b[col];
s=b[row];
}
return s;
}