Code:
// preprocessor directives
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <malloc.h>
#define ROWS 3
#define COLUMNS 4
#define SWAP_COUNT 4 * sizeof(float)
// swaps two equations: eq1 and eq2
#define SWAP_EQ(eq1, eq2) memcpy(temp,(eq1),SWAP_COUNT); memcpy((eq1),(eq2),SWAP_COUNT);memcpy((eq2),temp,SWAP_COUNT)
// multiplies two equations by the coefficients of variable var to eliminate var in equation eq
#define MULT_EQ(var,eq) for(j = 0; j < 4; j++) {temp[0][j] = matrix[(var)][j] * matrix[(eq - 3)][(var)]; temp[1][j] = matrix[(eq - 3)][j] * matrix[(var)][(var)];}
// eliminates the variable var in equation eq by subtracting the two equations in MULT_EQ
#define ELIM_VAR(eq) for(j = 0; j < 4; j++) matrix[(eq - 3)][j] = temp[0][j] - temp[1][j]
// function prototypes
// IN
// returns void and has one argument
void get_equations(float**matrix); // function that returns void and has one argument: float pointer to a pointer matrix named matrix.
// IN
// returns void and has one argument
void format_string(char*string); // function that returns void and has one argument: a character pointer named string.
// IN
// returns an integer flag 1 or 0 and has one argument
int check_equation(char*string); // function that returns an integer flag 1 or 0 and has one argument: a character pointer named string.
// IN
// returns void and has one argument
void validate_equations(float**matrix); // function that returns void and has one argument: float pointer to a pointer matrix named matrix.
//IN
// returns void and has one argument
void eliminate_variables(float**matrix); // function that returns void and has one argument: float pointer to a pointer matrix named matrix.
int main() // returns an int
{
// declare and initialize variables
int j; // loop counter
float **matrix; // pointer matrix that holds the numberical coefficienets of the equations
float x,y,z; // equations variables.
matrix = (float **) malloc(ROWS * sizeof(float **));
if(matrix == NULL) // or if (matrix == NULL)
{
printf("\nError - Memory allocation failure"); // display error message
exit(1); // abort the program
}
for(j = 0;j < ROWS; j++)
{
matrix[j] = (float *) malloc(COLUMNS * sizeof(float));
if(matrix[j]== NULL) // or if (matrix[j] == NULL)
{
// display error message
printf("\nError - Memory allocation failure");
exit(1); // abort the program
}
}
get_equations(matrix);
validate_equations(matrix);
eliminate_variables(matrix);
x = matrix[0][3] / matrix[0][0];
y = matrix[1][3] / matrix[1][1];
z = matrix[2][3] / matrix[2][2];
printf("x= %.2lf y= %.2lf z= %.2lf" , x, y, z); // come back when I define get equations
system("pause");
for(j = 0; j < ROWS; j++)
{
free(matrix[j]);
} // note only one statment so there was no need for {}
free(matrix);
system("pause");
return 0;
} // This is the end of main
// IN
// Descipitions
void get_equations(float**matrix)
{
int j, k;
char *string;
int flag;
string = (char *) malloc(35 * sizeof(char));
if(string == NULL) // or if (matrix[j] == NULL)
{
// display error message
printf("\nError - Memory allocation failure");
exit(1); // abort the program
}
GET_EQUATION_1:
printf("\nEnter 1st linear 3-variable equation e.g. x+4y-6z=20:");
fgets(string,80, stdin);
if(check_equation(string) != 1)
goto GET_EQUATION_1;
format_string(string);
sscanf(string, "%fx%fy%fz=%f", &matrix[0][0], &matrix[0][1], &matrix[0][2], &matrix[0][3]);
GET_EQUATION_2:
printf("\nEnter 2nd linear 3-variable equation e.g. x+4y-6z=20:");
fgets(string,80, stdin);
if(check_equation(string) != 1)
goto GET_EQUATION_2;
format_string(string);
sscanf(string, "%fx%fy%fz=%f", &matrix[1][0], &matrix[1][1], &matrix[1][2], &matrix[1][3]);
GET_EQUATION_3:
printf("\nEnter 3rd linear 3-variable equation e.g. x+4y-6z=20:");
fgets(string,80, stdin);
if(check_equation(string) != 1)
goto GET_EQUATION_3;
format_string(string);
sscanf(string, "%fx%fy%fz=%f", &matrix[2][0], &matrix[2][1], &matrix[2][2], &matrix[2][3]);
printf("\n\nEquation Matrix:\n\n");
for(j=0; j < ROWS; j++)
{
for(k=0; k < COLUMNS; k++)
{
printf("%.2f\t", matrix[j][k]);
}
printf("\n\n");
}
free(string);
} // end of function
// format string
void format_string(char *string)
{
unsigned int j, k;
for(j = 0; j < strlen(string); j++)
{
if(string[j] == ' ')
{
for(k = j; k < strlen(string) - 1; k++)
{
string[k] = string[k + 1];
}
}
}
for(j = 0; j < strlen(string); j++)
{
if(isalpha(string[j]) && j == 0) // may be wrong
{
for(k = strlen(string) + 1; k > j; k--) // decrements by 1 ??
{
string[k] = string[k - 1];
}
string[j] = '1';
}
if((string[j] == '+' || string[j] == '-') && isalpha(string[j + 1])) // may be wrong
{
for(k = strlen(string) + 1; k > j; k--) // not sure if I have decremented by 1
{
string[k] = string[k - 1];
}
string[j + 1] = '1';
}
}
} // end format_string( )
int check_equations(char*string)
{
unsigned int j;
int x, y, z, equals, operators, equal_found;
x = 0;
y = 0;
z = 0;
equals = 0;
operators = 0;
equal_found = 0;
for(j=0; j < strlen(string); j++)
{
string[j] = tolower(string[j]);
if(string[j] == 'x')
x++;
if(string[j] == 'y')
y++;
if(string[j] == 'z')
z++;
if(string[j] == '=')
{
equals++;
equal_found = 1;
}
if((string[j] == '+' || string[j] == '-') && equal_found == 0 && j != 0)
operators++;
}
if(x*y*z == 0 )
printf("\nEquation must contain all variables even if cofficients are zero.\n");
if(x > 1 || y > 1 || z > 1)
printf("\nEquation must not contain duplicate variables.\n");
if( equals != 1)
{
printf("\nEquation must contain one and only one equal sign\n");
equals = 0;
}
if(operators != 2)
{
printf("\nEquation has too few or too many +/- operators\n");
operators = 0;
}
else
operators = 1;
return x * y * z * equals * operators;
}
void validate_equations(float**matrix)
{
int j;
float temp[4];
if((matrix[0][0]/matrix[1][0] == matrix[0][1]/matrix[1][1] && matrix[1][0]/matrix[1][1] ==
matrix[0][2]/matrix[1][2] && matrix[0][2]/matrix[1][2] == matrix[0][3]/matrix[1][3]) ||
(matrix[0][0]/matrix[2][0] == matrix[1][0]/matrix[2][1] && matrix[1][0]/matrix[2][1] ==
matrix[0][2]/matrix[2][2] && matrix[0][2]/matrix[2][2] == matrix[0][3]/matrix[2][3]) ||
(matrix[1][0]/matrix[2][0] == matrix[1][1]/matrix[2][1] && matrix[1][1]/matrix[2][1] ==
matrix[1][2]/matrix[2][2] && matrix[1][2]/matrix[2][2] == matrix[1][3]/matrix[2][3]))
//a0/a1==b0/b1&& b0 /b1 == c0/c1 && c0/c1 ==d0/d1) 1 to 2
{
printf("\nOne or more equations are multiples and cannot be solved\n\n");
system("pause");
exit(-1);
}
if((matrix[0][0]/matrix[1][0] == matrix[0][1]/matrix[1][1] && matrix[1][0]/matrix[1][1] ==
matrix[0][2]/matrix[1][2] && matrix[0][2]/matrix[1][2] != matrix[0][3]/matrix[1][3]) ||
(matrix[0][0]/matrix[2][0] == matrix[1][0]/matrix[2][1] && matrix[1][0]/matrix[2][1] ==
matrix[0][2]/matrix[2][2] && matrix[0][2]/matrix[2][2] != matrix[0][3]/matrix[2][3]) ||
(matrix[1][0]/matrix[2][0] == matrix[1][1]/matrix[2][1] && matrix[1][1]/matrix[2][1] ==
matrix[1][2]/matrix[2][2] && matrix[1][2]/matrix[2][2] != matrix[1][3]/matrix[2][3]))
//a0/a1==b0/b1&& b0 /b1 == c0/c1 && c0/c1 !=d0/d1
{
printf("\nOne or more equations are inconsistent and cannot be solved\n\n");
system("pause");
exit(-1);
}
if((matrix[0][0]+ matrix[1][0] == matrix[2][2] && matrix[0][1] + matrix[1][1] == matrix[2][1] &&
matrix[0][2]+ matrix[1][2] == matrix[2][2] && matrix[0][3] + matrix[1][3] == matrix[2][3]) ||
(matrix[0][0]+ matrix[2][0] == matrix[1][2] && matrix[0][1] + matrix[2][1] == matrix[1][1] &&
matrix[0][2]+ matrix[2][2] == matrix[1][2] && matrix[0][3] + matrix[2][3] == matrix[1][3]) ||
(matrix[1][0]+ matrix[2][0] == matrix[0][2] && matrix[1][1] + matrix[2][1] == matrix[0][1] &&
matrix[1][2]+ matrix[2][2] == matrix[0][2] && matrix[1][3] + matrix[2][3] == matrix[0][3]))
//a0 + a1 == c2 && b0 + b1 == b2 && c0 + c1 == c2 && d0 + d1 == d2
{
printf("\nEquations are not linearly independent and cannot be solved\n\n");
system("pause");
exit(-1);
}
if( matrix[0][0] * matrix[1][1] * matrix[2][2] != 0)
{
// do nothing
}
else if( matrix[0][0] * matrix[2][1] * matrix[1][2] != 0)
{
SWAP_EQ(matrix[2],matrix[3]);
}
else if( matrix[1][0] * matrix[2][1] * matrix[0][2] != 0)
{
SWAP_EQ(matrix[1],matrix[2]);
}
else if( matrix[1][0] * matrix[0][1] * matrix[2][2] != 0)
{
SWAP_EQ(matrix[1],matrix[2]);
}
else if( matrix[2][0] * matrix[0][1] * matrix[1][2] != 0)
{
SWAP_EQ(matrix[2],matrix[3]);
SWAP_EQ(matrix[1],matrix[2]);
}
else if( matrix[2][0] * matrix[1][1] * matrix[0][2] != 0)
{
SWAP_EQ(matrix[1],matrix[3]);
}
else
{
printf("\nError: Insufficient number of variable coefficients to solve the equations\n\n");
abort ();
}
}
void eliminate_variables(float**matrix)
{
int j;
float temp [2][4];
enum words {var_x = 0, var_y = 1, var_z = 2, eq_1 = 3, eq_2 = 4, eq_3 = 5};
MULT_EQ(var_x, eq_2);
ELIM_VAR(var_x);
MULT_EQ(var_x, eq_3);
ELIM_VAR(var_x);
MULT_EQ(var_y, eq_1);
ELIM_VAR(var_y);
MULT_EQ(var_y, eq_3);
ELIM_VAR(var_y);
MULT_EQ(var_z, eq_1);
ELIM_VAR(var_z);
MULT_EQ(var_z, eq_2);
ELIM_VAR(var_z);
}
Please excuse the loose formatting .. I will be cleaning this up before I submittal. But does anyone know why I am getting these errors?