/* This source file will compile by itself.  It was used by me to work
   out bugs with my SolveMatrix function.  The reason I used this file
   was becuase it uses arrays instead of pointers to do the work.
   Pointers are far more flexible, however, when working out bugs in a
   program, they can add confusion that I just didn't need.  Also
   this source file should serve nicely to let people who are interested
   know what this routine is doing.  It is far easier to grasp than
   the function I wrote for general use.  The extra printf statments
   should help the casual programmer wade through this mess.  This file
   is included for conceptual understanding only.  If you would like to
   use this code in your program, I would highly recomend you use the
   SolveMatrix.c code instead.
*/


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

#define clrscr() printf("\f");
#define MAXROWS 9

void scanmatrix(float matrix[MAXROWS][MAXROWS+1], int *rows);
void printmatrix(float matrix[MAXROWS][MAXROWS+1], int rows);
void solvematrix(float matrix[MAXROWS][MAXROWS+1], int rows);

int main(void)
{
   int x, rows;
   float matrix[MAXROWS][MAXROWS+1] = {{0}};

   scanmatrix(matrix, &rows);  
   printmatrix(matrix, rows);
   solvematrix(matrix, rows);  
                               
                                                  
   printf("\n");                                  
   for (x = 0; x < rows; x++)                     
   {                                              
      printf("x^%d = %f\n", x, matrix[x][rows]);  
   }                                             
   exit(0);
}

void scanmatrix(float matrix[MAXROWS][MAXROWS+1], int *rows)
{
   register int x, y;

   do
   {
      clrscr();
      printf("Enter the number of rows... (May not exceed %d)", MAXROWS);
      scanf("%d", rows);
   } while ((*rows<=1) || (*rows > MAXROWS));
   printf("Enter %d numbers to make a matrix.\n", *rows*(*rows+1));
   printf("The first group of numbers keyed in will be the first row.\n");
   printf("The second group of numbers will be the second row, etc...\n");
   printf("The numbers must be separated by hitting <ENTER>.\n");
   for (y = 0; y < *rows; y++)
   {
      for (x = 0; x<=*rows; x++)
      {
         scanf("%f", &matrix[y][x]);
      }
   }
}

void printmatrix(float matrix[MAXROWS][MAXROWS+1], int rows)
{
   register int x, y;

   clrscr();

   for (y = 0; y < rows; y++)
   {
      for (x = 0; x<=rows; x++)
      {
         printf("%10.2f", matrix[y][x]);
      }
      printf("\n");
   }
   printf("\nHit <ENTER> to continue...\n");
   while (getch()!=13) ;
}

void solvematrix(float matrix[MAXROWS][MAXROWS+1], int rows)
{
   register int x, y, z;       
   float temp1, temp2;

   for (z = 0; z < rows-1; z++)
   {
      for (y = rows-2; y>=z; y--)
      {
         if (matrix[y+1][z])
         {
            temp2 = matrix[y+1][z];

            if (matrix[y][z]==0) for (x = 0; x<=rows; x++)
               matrix[y][x] = matrix[y+1][x] - matrix[y][x];

            temp1 = matrix[y][z];
            for (x = 0; x<=rows; x++)
            {
               matrix[y][x] *= temp2;
               printmatrix(matrix, rows);
               matrix[y+1][x] *= temp1;
               printmatrix(matrix, rows);
               matrix[y+1][x] = matrix[y][x] - matrix[y+1][x];
               printmatrix(matrix, rows);
               matrix[y][x] /= temp2;
               printmatrix(matrix, rows);
            }
         }

         for (x = 0; x < rows && matrix[y+1][x]==0; x++) ;
         if (x >= rows)
         {
            if (matrix[y+1][rows]) printf("\nNo solution set.\n");
            else printf("\nInfinite solution set.\n");
            exit(0);
         }
      }
   }

   for (y = rows-1; y>=0; y--)
   {
      for (x = rows-1; x>=y; x--)
      {
         if (matrix[x][x]==1 && x==y) break;
         else if (matrix[x][x]==1)
         {
            matrix[y][x] *= matrix[x][rows];
            matrix[y][rows] -= matrix[y][x];
            matrix[y][x] = 0;
         }
         else
         {
            matrix[y][rows] = matrix[y][rows] / matrix[y][x];
            matrix[y][x] = 1;
         }
         printmatrix(matrix, rows);
      }
   }
}

