# Read coefficients of linear equations from file and use jacobi method

This is a discussion on Read coefficients of linear equations from file and use jacobi method within the C Programming forums, part of the General Programming Boards category; Hi, I did a function that get inputs of equations from user but I am trying to get from a ...

1. ## Read coefficients of linear equations from file and use jacobi method

Hi,

I did a function that get inputs of equations from user but I am trying to get from a file

Code:
```void getInput( int numUnKnowns, double** mat ){
int i, j;

printf
(
"\nEnter values for the specified row and column below ...\n"
"(The last column is the value for the RHS of the equation.)\n"
);

for( i = 0 ; i < numUnKnowns ; i++ )
{
mat[i] = (double*) malloc( (numUnKnowns+1)*sizeof(double) );
puts("");
for( j = 0 ; j < numUnKnowns+1 ; j++ )
{
printf("matrix[%d][%d] : ", i, j);
if( scanf("%lf", &mat[i][j]) != 1 )
{
--j;
puts("Bad entry ... try again with a 'real' number.");
}
while( getchar() != '\n' ) ; /* flush stdin ... */
}
}

printf("\nThe matrix entered:\n\n");
for( i = 0 ; i < numUnKnowns ; i++ )
{
for( j = 0 ; j < numUnKnowns+1 ; j++ ) printf("%+9f ", mat[i][j]);
puts("");
}

printf("\nPress 'Enter' to start iteration ... ");
getchar();
}```

can anyone help me how the function read the linear equation from file. assume the file has:
6x + 2y -z = 5
-2x + 4y + z = 8
3x + y + 7z = 3

2. jacobi function

Code:
```void jacobiCalcDisplay( int numUnKnowns, double** mat ){
int* flag;
int i, j, counter = 0;
double* res;
double* var = (double*) malloc( numUnKnowns*sizeof(double) );
res = (double*) malloc( numUnKnowns*sizeof(double) );
flag = (int*) malloc( numUnKnowns*sizeof(int) );

for(i = 0 ; i < numUnKnowns ; i++ )
var[i] = res[i] = flag[i] = 0;
printf("The initial value of each array element was set to zero ...\n\n");

printf( "*********************\n");
printf( "START CALCULATING ...\n");
printf( "*********************\n");

do
{
counter++;
/* for each iteration keep a copy of the old results ... */
for(i = 0 ; i < numUnKnowns ; i++ )
{
var[i] = res[i];
}

if( SHOW_ITERS ) printf("\nIteration number %d ...\n", counter);

for(i = 0 ; i < numUnKnowns ; i++ ) /* calculation */
{
res[i] = mat[i][numUnKnowns];
for(j = 0 ; j < numUnKnowns ; j++ )
res[i] = res[i] - mat[i][j]*var[j] ;

res[i] = res[i] + mat[i][i]*var[i] ;
res[i] = res[i] / mat[i][i] ;
if( SHOW_ITERS ) printf("%c = %f\n", 'a'+i, res[i]);
if( fabs(res[i] - var[i]) < EPSILON ) /* stop condition */
flag[i]++;

if( counter==MAX_PRINT_ITERS) SHOW_ITERS = 0;
}

}while( !checkFlags( numUnKnowns, flag ) );

printf( "\n********************************\n");
printf( "The RESULTS of %d ITERATIONS ... \n", counter);
printf( "********************************\n");

/*  cross check ...*/

for( i = 0 ; i < numUnKnowns ; i++)
var[i] = dotProd( numUnKnowns, mat[i], res );

showXcheck( numUnKnowns, mat, res, var );

/* show sol'n vector (again) ... and free up all dynamic memory  */

printf("\nSolution vector ...\n");
for( i = 0 ; i < numUnKnowns ; i++)
{
printf("%c = %+f\n", 'a'+i, res[i]);
free(mat[i]);
}
free( mat );
free( flag );
free( res );
free( var );
}```

3. I did a function that get inputs of equations from user but I am trying to get from a file
No, you found some code that approximates what you need, and now, because you're nothing but a scoop and poop programmer, you're looking for us to give you the code to give you exactly what you need. Pretty sad.

4. sorry, I did my assignment by myself
I can post all my work.