/* Program to solve a system of linear equations with 'n' variables */

/*  This algorithm makes use of the Gauss-Jordan method of solving simultaneous linear equations */

/* For any clarifications please mail me at vaivaswatha@puttu.net */

#include <stdio.h>
#define MAX 5

main()
{
	int order, matrix[MAX][MAX+1], test = 1;
	void exit();
	
	do 
	{
		printf("Maximum of %d variables. How many variables? ", MAX);
		scanf("%d", &order);
	}
	while (order > MAX);
	
	printf("\na11(x1) + a12(x2) + ..... + a1n(xn) = d1\n\n");
	printf("Enter the co-efficients one by one....\n");
	
	input (matrix, order);
	test = verify_validity (matrix, order); /* to ensure non-zero co-efficents for variables */
 	if (test == 0)
	{
		printf("This program supports only non-zero co-efficients for variables...\n");
		exit(0);
	}
		
	make_lower_zero (matrix, order);
	output2 (matrix, order);
	
	rotate_180 (matrix, order);
	output2 (matrix, order);
	
	make_lower_zero (matrix, order);
	output2 (matrix, order);
	
	rotate_180 (matrix, order);
			
	output2 (matrix, order);
	
	output_result (matrix, order);
}

int input (matrix, order)
int matrix[MAX][MAX+1], order;
{
			
	int i, j;
	
	for (i = 0; i < order; i++)
		for (j = 0; j < (order + 1); j++)
			scanf ("%d", &matrix[i][j]);
	return 0;
}

int output_result (matrix, order)
int matrix[MAX][MAX+1], order;
{
	int i; float answers[MAX];
	void exit();
	
	for (i = 0; i < order; i++)
		if (matrix[i][i] != 0)
			answers[i] = ((float) matrix[i][order] / (matrix[i][i]));
		else 
		{
			printf("No unique solutions....\n");
			exit (0);
		}
	for (i = 0; i < order; i++)
		printf("Variable #%2d: %f\n", i+1, answers[i]);
	return 0;
}

int make_lower_zero (matrix, order)
int matrix[MAX][MAX+1], order;
{
	int home, zero, lcm;
	
	for (home = 0; home < order-1; home++)
		for (zero = home + 1; zero < order; zero++)
		{
 			int factor_home, factor_zero;
			lcm = find_lcm (matrix[home][home], matrix[zero][home]);
			factor_home = mod(lcm)/mod(matrix[home][home]);
			factor_zero = lcm/mod(matrix[zero][home]);
			multiply_by (matrix[home], factor_home, order);
			multiply_by (matrix[zero], factor_zero, order);
			subtract_home_row (matrix[zero], matrix[home], order);
			divide_by (matrix[home], factor_home, order);
			
		}
	return 0;
}
			
int multiply_by (matrix, a, order)
int matrix[MAX], a, order;
{
	int i;
	
	for (i = 0; i < order+1; i++)   /* i+1 is used to include the constant term d */
		matrix[i] = (matrix[i] * a);
	return 0;
}

int divide_by (matrix, a, order)
int matrix[MAX], a, order;
{
	int i;
	
	for (i = 0; i < order+1; i++)   /* i+1 is used to include the constant term d */
		matrix[i] = (matrix[i] / a);
	return 0;
}

int subtract_home_row (array2, array1, order)
int array2[], array1[], order;
{
	int i;
	
	for (i = 0; i < order+1; i++)   /* i+1 is used to include the constant term d */
		array2[i] = array2[i] - array1[i];
	return 0;
}

int mod (a)
int a;
{
	if (a < 0)
		return (-1)*a;
	else
		return a;
}

int find_lcm (a, b)
int a, b;
{
	int temp, test = 1;
	if (a < 0)
	{
		a = a*(-1);
		test = test*(-1);
	}
	if (b < 0)
	{
		b = b*(-1);
		test = test*(-1);
	}
	if (b > a)
	{
		temp = b;
		for (; (b % a) != 0; b = (b + temp));
		return b*(test);	
	}
	else
	{
		temp = a;
		for (; (a % b) != 0; a = (a + temp));
		return a*(test);
	}
}

int rotate_180 (matrix, order)
int matrix[MAX][MAX+1], order;
{
	int i, j, k;
	
	for (i = 0, j = order-1; i <= j; i++, j--)
	{
		column_reverse (matrix[i], order);
		if (i != j)
			column_reverse (matrix[j], order);
		
		for (k = 0; k < order+1; k++)  /* reverse rows */
		{
			int temp;
			temp = matrix[i][k];
			matrix[i][k] = matrix[j][k];
			matrix[j][k] = temp;
		}
	}
	return 0;
}

int output2 (matrix, order)
int matrix[MAX][MAX+1];
{
	int i, j;
	
	printf("\n");
	for (i = 0; i < order; i++)
	{
		for (j = 0; j < order+1; j++)
			printf("%4d ", matrix[i][j]);
		printf("\n");
	}
	printf("\n");
	return 0;
}

int column_reverse (array, order)
int array[], order;
{
	int i, j;
	
	for (i = 0, j = order-1; i < j; i++, j--)
	{
		int temp;
		temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}
	
	return 0;
}

int verify_validity (matrix, order)
int matrix[MAX][MAX+1], order;
{
	int i, j, test = 1;
	
	for (i = 0; i < order; i++)
		for (j = 0; j < order; j++)
			if (matrix[i][j] == 0)
				test = 0;
	return test;			
}
www.000webhost.com