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 );
}