Can any one help me to run this program? I am trying to modify it for multiple linear regression and use Gaussian elimination to solve the matrix.I found this in a book but it is not working:(Does anyone have anything related?I am a beginner so please something not so complicated

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

/* -------------------------------------------------------- */

/*  Main program for algorithm 5.2  */

/*  remember : in C the fields begin with element 0  */

#define DMAX  15    /* Maximum degree of polynomial */
#define NMAX  20    /* Maximum number of points     */


void main(void)
{
    extern void FactPiv();

    int R, K, J;                     /* Loop counters     */
    double X[NMAX-1], Y[NMAX-1];     /* Points (x,y)      */
    double A[DMAX][DMAX];            /* A                 */
    double B[DMAX];                  /* B                 */
    double C[DMAX]; 
    double P[2*DMAX];
    int N;                           /* Number of points :     INPUT */   
    int M;                           /* Degree of polynomial : INPUT */
    double x, y;
    int p;


    printf("Try the examples on page 277 or 281 of the book !\n");
    printf("-------------------------------------------------\n");

    do  /* force proper input */
    {
      printf("Please enter degree of polynomial [Not more than %d]\n",DMAX);
      scanf("%d", &M);
    } while( M > DMAX);

    printf("------------------------------------------\n");
    do  /* force proper input */
    {
      printf("Please enter number of points [Not more than %d]\n",NMAX);
      scanf("%d", &N);
    } while( N > NMAX);

    printf("You say there are %d points.\n", N);
    printf("-----------------------------------------------------\n");
    printf("Enter points in pairs like :  2.4, 4.55:\n");

    for (K = 1; K <= N; K++)
    {
      printf("Enter %d st/nd/rd pair of points:\n", K);
      scanf("%lf, %lf", &X[K-1], &Y[K-1]);
      printf("You entered the pair (x,y) = %lf, %lf\n", X[K-1], Y[K-1]);
    }


    /* Zero the array */

    for (R = 1; R <= M+1; R++) B[R-1] = 0;      

    /* Compute the column vector */

    for (K = 1; K <= N; K++)           
    {
      y = Y[K-1];
      x = X[K-1];
      p = 1; 

      for( R = 1; R <= M+1; R++ )
      {
         B[R-1] += y * p;
         p = p*x;
      }
    }
        
    /* Zero the array */

    for (J = 1; J <= 2*M; J++) P[J] = 0;
    
    P[0] = N;

    /* Compute the sum of powers of x_(K-1) */

    for (K = 1; K <= N; K++)
    {
        x = X[K-1];
        p = X[K-1];
 
        for (J = 1; J <= 2*M; J++)
        {
            P[J] += p;
            p = p * x;
        }
    }

    /* Determine the matrix entries */

    for (R = 1; R <= M+1; R++)
    {
        for( K = 1; K <= M+1; K++) A[R-1][K-1] = P[R+K-2];
    }



    /* Solve the linear system of M + 1 equations : A*C = B 
       for the coefficient vector C = (c_1,c_2,..,c_M,c_(M+1)) */


    FactPiv(M+1, A, B);

}   /* end main */

/*--------------------------------------------------------*/

void FactPiv(N, A, B)
int N;
double A[DMAX][DMAX];
double *B;
{

    int K, P, C, J;                  /* Loop counters               */
    int    Row[NMAX];                /* Field with row-number       */
    double X[DMAX], Y[DMAX
    ];
    double SUM, DET = 1.0;                                                      
 
    int  T;                               
    

    /* Initialize the pointer vector */

    for (J = 1; J<= N; J++) Row[J-1] = J - 1;



    /* Start LU factorization */

    for (P = 1; P <= N - 1; P++)
    {

      /* Find pivot element */  

      for (K = P + 1; K <= N; K++)
      {
        if ( fabs(A[Row[K-1]][P-1]) > fabs(A[Row[P-1]][P-1]) )
        {
        /* Switch the index for the p-1 th pivot row if necessary */ 
           T        = Row[P-1];
           Row[P-1] = Row[K-1];
           Row[K-1] = T;
           DET      = - DET; 
        }


      } /* End of simulated row interchange */

        if (A[Row[P-1]][P-1] == 0)
        {   
           printf("The matrix is SINGULAR !\n");
           printf("Cannot use algorithm --> exit\n");
           exit(1);
        }

     /* Multiply the diagonal elements */
     
     DET = DET * A[Row[P-1]][P-1];

     /* Form multiplier */

     for (K = P + 1; K <= N; K++)
     { 
       A[Row[K-1]][P-1]= A[Row[K-1]][P-1] / A[Row[P-1]][P-1];
      
       /* Eliminate X_(p-1) */

       for (C = P + 1; C <= N + 1; C++)
       {
          A[Row[K-1]][C-1] -= A[Row[K-1]][P-1] * A[Row[P-1]][C-1];
       }
     } 

  } /* End of  L*U  factorization routine */


    DET = DET * A[Row[N-1]][N-1];

 

    /* Start the forward substitution */

    for(K = 1; K <= N; K++) Y[K-1] = B[K-1];

    Y[0] = B[Row[0]]; 
    for ( K = 2; K <= N; K++)
    {
      SUM =0;
      for ( C = 1; C <= K -1; C++) SUM += A[Row[K-1]][C-1] * Y[C-1];
      Y[K-1] = B[Row[K-1]] - SUM;
    }  


    if( A[Row[N-1]][N-1] == 0)
    {
      printf("The matrix is SINGULAR !\n");
      printf("Cannot use algorithm --> exit\n");
      exit(1);
    }
      

    /* Start the back substitution */
   
    X[N-1] = Y[N-1] / A[Row[N-1]][N-1];

    for (K = N - 1; K >= 1; K--)
    {
      SUM = 0;
      for (C = K + 1; C <= N; C++)
      {
           SUM += A[Row[K-1]][C-1] * X[C-1];   
      }

      X[K-1] = ( Y[K-1] - SUM) / A[Row[K-1]][K-1];

    }  /* End of back substitution */

    /* Output */

    printf("---------------------------------------------------:\n");
    printf("The components of the vector with the solutions are:\n");
    for( K = 1; K <= N; K++)  printf("X[%d] = %lf\n", K, X[K-1]); 

}

Edited 5 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

/* -------------------------------------------------------- */

/*  Main program for algorithm 5.2  */

/*  remember : in C the fields begin with element 0  */

#define DMAX  15    /* Maximum degree of polynomial */
#define NMAX  20    /* Maximum number of points     */


int main(void)
{
    extern void FactPiv();

    int R, K, J;                     /* Loop counters     */
    double X[NMAX-1], Y[NMAX-1];     /* Points (x,y)      */
    double A[DMAX][DMAX];            /* A                 */
    double B[DMAX];                  /* B                 */
    double C[DMAX]; 
    double P[2*DMAX];
    int N;                           /* Number of points :     INPUT */   
    int M;                           /* Degree of polynomial : INPUT */
    double x, y;
    int p;


    printf("Try the examples on page 277 or 281 of the book !\n");
    printf("-------------------------------------------------\n");

    do  /* force proper input */
    {
      printf("Please enter degree of polynomial [Not more than %d]\n",DMAX);
      scanf("%d", &M);
    } while( M > DMAX);

    printf("------------------------------------------\n");
    do  /* force proper input */
    {
      printf("Please enter number of points [Not more than %d]\n",NMAX);
      scanf("%d", &N);
    } while( N > NMAX);

    printf("You say there are %d points.\n", N);
    printf("-----------------------------------------------------\n");
    printf("Enter points in pairs like :  2.4, 4.55:\n");

    for (K = 1; K <= N; K++)
    {
      printf("Enter %d st/nd/rd pair of points:\n", K);
      scanf("%lf, %lf", &X[K-1], &Y[K-1]);
      printf("You entered the pair (x,y) = %lf, %lf\n", X[K-1], Y[K-1]);
    }


    /* Zero the array */

    for (R = 1; R <= M+1; R++) B[R-1] = 0;      

    /* Compute the column vector */

    for (K = 1; K <= N; K++)           
    {
      y = Y[K-1];
      x = X[K-1];
      p = 1; 

      for( R = 1; R <= M+1; R++ )
      {
         B[R-1] += y * p;
         p = p*x;
      }
    }
        
    /* Zero the array */

    for (J = 1; J <= 2*M; J++) P[J] = 0;
    
    P[0] = N;

    /* Compute the sum of powers of x_(K-1) */

    for (K = 1; K <= N; K++)
    {
        x = X[K-1];
        p = X[K-1];
 
        for (J = 1; J <= 2*M; J++)
        {
            P[J] += p;
            p = p * x;
        }
    }

    /* Determine the matrix entries */

    for (R = 1; R <= M+1; R++)
    {
        for( K = 1; K <= M+1; K++) A[R-1][K-1] = P[R+K-2];
    }



    /* Solve the linear system of M + 1 equations : A*C = B 
       for the coefficient vector C = (c_1,c_2,..,c_M,c_(M+1)) */


    FactPiv(M+1, A, B);

    return 0;

}   /* end main */

/*--------------------------------------------------------*/

void FactPiv(N, A, B)
int N;
double A[DMAX][DMAX];
double *B;
{

    int K, P, C, J;                  /* Loop counters               */
    int    Row[NMAX];                /* Field with row-number       */
    double X[DMAX], Y[DMAX
    ];
    double SUM, DET = 1.0;                                                      
 
    int  T;                               
    

    /* Initialize the pointer vector */

    for (J = 1; J<= N; J++) Row[J-1] = J - 1;



    /* Start LU factorization */

    for (P = 1; P <= N - 1; P++)
    {

      /* Find pivot element */  

      for (K = P + 1; K <= N; K++)
      {
        if ( fabs(A[Row[K-1]][P-1]) > fabs(A[Row[P-1]][P-1]) )
        {
        /* Switch the index for the p-1 th pivot row if necessary */ 
           T        = Row[P-1];
           Row[P-1] = Row[K-1];
           Row[K-1] = T;
           DET      = - DET; 
        }


      } /* End of simulated row interchange */

        if (A[Row[P-1]][P-1] == 0)
        {   
           printf("The matrix is SINGULAR !\n");
           printf("Cannot use algorithm --> exit\n");
           exit(1);
        }

     /* Multiply the diagonal elements */
     
     DET = DET * A[Row[P-1]][P-1];

     /* Form multiplier */

     for (K = P + 1; K <= N; K++)
     { 
       A[Row[K-1]][P-1]= A[Row[K-1]][P-1] / A[Row[P-1]][P-1];
      
       /* Eliminate X_(p-1) */

       for (C = P + 1; C <= N + 1; C++)
       {
          A[Row[K-1]][C-1] -= A[Row[K-1]][P-1] * A[Row[P-1]][C-1];
       }
     } 

  } /* End of  L*U  factorization routine */


    DET = DET * A[Row[N-1]][N-1];

 

    /* Start the forward substitution */

    for(K = 1; K <= N; K++) Y[K-1] = B[K-1];

    Y[0] = B[Row[0]]; 
    for ( K = 2; K <= N; K++)
    {
      SUM =0;
      for ( C = 1; C <= K -1; C++) SUM += A[Row[K-1]][C-1] * Y[C-1];
      Y[K-1] = B[Row[K-1]] - SUM;
    }  


    if( A[Row[N-1]][N-1] == 0)
    {
      printf("The matrix is SINGULAR !\n");
      printf("Cannot use algorithm --> exit\n");
      exit(1);
    }
      

    /* Start the back substitution */
   
    X[N-1] = Y[N-1] / A[Row[N-1]][N-1];

    for (K = N - 1; K >= 1; K--)
    {
      SUM = 0;
      for (C = K + 1; C <= N; C++)
      {
           SUM += A[Row[K-1]][C-1] * X[C-1];   
      }

      X[K-1] = ( Y[K-1] - SUM) / A[Row[K-1]][K-1];

    }  /* End of back substitution */

    /* Output */

    printf("---------------------------------------------------:\n");
    printf("The components of the vector with the solutions are:\n");
    for( K = 1; K <= N; K++)  printf("X[%d] = %lf\n", K, X[K-1]); 



}

Use int main() PLEASE :)

This article has been dead for over six months. Start a new discussion instead.