home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / ANUMR5.ZIP / GSDLTEST.C < prev    next >
C/C++ Source or Header  |  1991-03-10  |  2KB  |  104 lines

  1. /* OK */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <mem.h>
  7. #include <alloc.h>
  8. #include <dos.h>
  9. #include <conio.h>
  10.  
  11.  
  12. #include "anum.h"
  13. #include "sysio.h"
  14.  
  15. #define MAXDIM 5
  16.  
  17.  
  18. void affmat(int dim, double *matrix)
  19.  
  20.  
  21. {    int i,j;
  22.  
  23.     for (i=0;i<dim;i++)
  24.     {    for (j=0;j<dim;j++) printf("%8.4g ",*(matrix+i*dim+j));
  25.         putchar('\n');
  26.     }
  27. }
  28.  
  29.  
  30. void affvect(int dim, double *vect)
  31.  
  32.  
  33. {    int i;
  34.  
  35.     for (i=0;i<dim;)
  36.         printf("[%d] = %8.4g\n",++i,*(vect+i));
  37.  
  38. }
  39.  
  40.  
  41.  
  42. void results(int dim, double *vsol)
  43.  
  44. {    puts("Solution of A.X=B :");
  45.     affvect(dim,vsol);
  46.     printf("Core left : %ld bytes.\n",farcoreleft());
  47. }
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. void main(void)
  55.  
  56.  
  57. {    double *mat_A, *vect_B, *vect_X;
  58.     double tol=1.e-07;
  59.     int maxiter=12500;
  60.     int iter;
  61.     int dim,i,j,errcode;
  62.  
  63.  
  64.     ena_m_beep();
  65.         clrscr();
  66.     puts("Test program for function gauss_seidel");
  67.     puts("--------------------------------------\n");
  68.  
  69.     for (dim=1;dim<=MAXDIM;dim++)
  70.     {    mat_A=farcalloc(dim*dim,sizeof(double));
  71.         vect_B=farcalloc(dim,sizeof(double));
  72.         vect_X=farcalloc(dim,sizeof(double));
  73.  
  74.         for (i=0;i<dim;i++)
  75.         {    *(vect_B+i)=(double)(rand()%(10*(1+dim / 4))
  76.                             -5 * (1+dim/4));
  77.             *(vect_X+i)=(double)(rand()%(10*(1+dim/4))
  78.                             -5*(1+dim/4));
  79.             for(j=0;j<dim;j++)
  80.                 *(mat_A+i*dim+j)=(double)(rand()%10*dim
  81.                             -5*dim);
  82.         }
  83.  
  84.         printf("\nMatrix order %d\n",dim);
  85.         puts("Matrix A");
  86.         affmat(dim,mat_A);
  87.         puts("\nVector B");
  88.         affvect(dim, vect_B);
  89.  
  90.         gauss_seidel(dim, mat_A, vect_B, tol, maxiter, vect_X,
  91.                  &iter , &errcode);
  92.         SYSMSG(errcode,stderr);
  93.  
  94.         results(dim,vect_X);
  95.         farfree(mat_A);
  96.         farfree(vect_X);
  97.         farfree(vect_B);
  98.     }
  99. }
  100.  
  101.  
  102.  
  103.  
  104.