#include #define NUM_OF_EQUATIONS 3 #define NUM_OF_TERMS 6 double equationOneVars[] = { 5, 2, 1, 0, 0, 20 }; double equationTwoVars[] = { 5, 8, 0, 1, 0, 50 }; double equationThreeVars[] = { -1, 2, 0, 0, 1, 0 }; double terms[NUM_OF_TERMS * NUM_OF_EQUATIONS]; double difTerms[(NUM_OF_TERMS - 1) * NUM_OF_EQUATIONS]; double matrix[NUM_OF_EQUATIONS][NUM_OF_TERMS]; void printMatrix(char* a, char* b, char* c, char* d, char* e) { printf("\n%s %s %s %s %s\n", a, b, c, d, e); for (int i = 0; i < NUM_OF_EQUATIONS; i++) { for (int j = 0; j < NUM_OF_TERMS; j++) { printf("%f ", matrix[i][j]); } printf("\n"); } printf("\n"); } void makeOtherMatrix() { double oldMatrix[NUM_OF_EQUATIONS][NUM_OF_TERMS]; int k = 0; for (int i = 0; i < NUM_OF_EQUATIONS; i++) { for (int j = 0; j < NUM_OF_TERMS; j++) { oldMatrix[i][j] = terms[k]; k++; } } double difMatrix[NUM_OF_EQUATIONS][NUM_OF_TERMS - 1]; k = 0; for (int i = 0; i < NUM_OF_EQUATIONS; i++) { for (int j = 0; j < NUM_OF_TERMS - 1; j++) { difMatrix[i][j] = difTerms[k]; k++; } } } void findNegative(int* negativeCol, double* negativeNum) { *negativeNum = 0; *negativeCol = 0; for (int i = 0; i < NUM_OF_TERMS; i++) { if (matrix[NUM_OF_EQUATIONS - 1][i] < *negativeNum) { *negativeNum = matrix[NUM_OF_EQUATIONS - 1][i]; *negativeCol = i; } } } int findSmallQuotient(int col) { int smallestRow = 0; double smallestNum = __DBL_MAX__; for (int i = 0; i < NUM_OF_EQUATIONS; i++) { if (matrix[i][NUM_OF_TERMS - 1] != 0 && matrix[i][col] != 0 && i != (NUM_OF_EQUATIONS - 1) && (matrix[i][NUM_OF_TERMS - 1] / matrix[i][col]) < smallestNum) { smallestNum = (matrix[i][NUM_OF_TERMS - 1] / matrix[i][col]); smallestRow = i; } } return smallestRow; } void start() { int col; double negativeNum; findNegative(&col, &negativeNum); while (negativeNum < 0) { int row = findSmallQuotient(col); multiplyRow(row, (1 / matrix[row][col])); (void)printf("%d %d %f get_ready\n", row, col, (1 / matrix[row][col])); findNegative(&col, &negativeNum); for (int i = 0; i < NUM_OF_EQUATIONS; i++) { if (i != row) { makeTermsZero(i, col, row); (void)printf("%d %d %d get_Freddy\n", i, col, row); } } findNegative(&col, &negativeNum); } } void makeTermsZero(int row, int col, int rowTwo) { addMultiplyRows(row, rowTwo, -1 * matrix[row][col]); } void switchRows(int rowNum, int rowNumTwo) { double subRow[NUM_OF_TERMS]; for (int i = 0; i < NUM_OF_TERMS; i++) { subRow[i] = matrix[rowNumTwo][i]; } for (int i = 0; i < NUM_OF_TERMS; i++) { matrix[rowNumTwo][i] = matrix[rowNum][i]; } for (int i = 0; i < NUM_OF_TERMS; i++) { matrix[rowNum][i] = subRow[i]; } } void multiplyRow(int rowNum, double mult) { for (int i = 0; i < NUM_OF_TERMS; i++) { matrix[rowNum][i] = mult * matrix[rowNum][i]; } } void addMultiplyRows(int rowNum, int rowNumTwo, double mult) { for (int i = 0; i < NUM_OF_TERMS; i++) { matrix[rowNum][i] = matrix[rowNum][i] + (mult * matrix[rowNumTwo][i]); } } int main() { for (int i = 0; i < NUM_OF_TERMS; i++) { terms[i] = equationOneVars[i]; terms[NUM_OF_TERMS + i] = equationTwoVars[i]; terms[2 * NUM_OF_TERMS + i] = equationThreeVars[i]; } int j = 0; for (int i = 0; i < NUM_OF_EQUATIONS; i++) { if (i % NUM_OF_TERMS != 0) { difTerms[j] = terms[i]; j++; } } int k = 0; for (int i = 0; i < NUM_OF_EQUATIONS; i++) { for (int j = 0; j < NUM_OF_TERMS; j++) { matrix[i][j] = terms[k]; k++; } } printMatrix("", "", "", "", ""); start(); printMatrix("", "", "", "", ""); return 0; }