From: Soikk <76824648+Soikk@users.noreply.github.com> Date: Sun, 24 Oct 2021 22:52:13 +0000 (+0200) Subject: Bug fixes, UI fixes X-Git-Url: https://git.xolatile.top/?a=commitdiff_plain;ds=sidebyside;p=soikk-matrix-calculator.git Bug fixes, UI fixes You can now raise a matrix to a negative power and store the resulting matrixes from an operation with a scalar in another matrix --- diff --git a/matrix-calculator.c b/matrix-calculator.c index a9cebd8..06b90f6 100644 --- a/matrix-calculator.c +++ b/matrix-calculator.c @@ -290,69 +290,99 @@ void matrixCalculator(int input){ case FILL: printf("What matrix would you like to fill with a number?\n"); + do{ + printf("Matrix A (0), B (1) or C (2)? "); + scanf("%d", &d); + }while(d < 0 || d > 2); + printf("Where would you like to store the resulting matrix?\n"); do{ printf("Matrix A (0), B (1) or C (2)? "); scanf("%d", &s); }while(s < 0 || s > 2); printf("With which number would you like to fill matrix %s?\n", - (s==0)?"A":(s==1)?"B":"C"); + (d==0)?"A":(d==1)?"B":"C"); scanf("%f", &n); - matrices[s] = fillN(matrices[s], n); + matrices[s] = fillN(matrices[d], n); break; case ADD_N: printf("What matrix would you like to add a number to?\n"); + do{ + printf("Matrix A (0), B (1) or C (2)? "); + scanf("%d", &d); + }while(d < 0 || d > 2); + printf("Where would you like to store the resulting matrix?\n"); do{ printf("Matrix A (0), B (1) or C (2)? "); scanf("%d", &s); }while(s < 0 || s > 2); printf("With which number would you like to add to matrix %s?\n", - (s==0)?"A":(s==1)?"B":"C"); + (d==0)?"A":(d==1)?"B":"C"); scanf("%f", &n); - matrices[s] = addN(matrices[s], n); + matrices[s] = addN(matrices[d], n); break; case SUBSTRACT_N: printf("What matrix would you like to substract a number from?\n"); + do{ + printf("Matrix A (0), B (1) or C (2)? "); + scanf("%d", &d); + }while(d < 0 || d > 2); + printf("Where would you like to store the resulting matrix?\n"); do{ printf("Matrix A (0), B (1) or C (2)? "); scanf("%d", &s); }while(s < 0 || s > 2); printf("With which number would you like to substract from matrix %s?\n", - (s==0)?"A":(s==1)?"B":"C"); + (d==0)?"A":(d==1)?"B":"C"); scanf("%f", &n); - matrices[s] = substractN(matrices[s], n); + matrices[s] = substractN(matrices[d], n); break; case MULTIPLY_N: printf("What matrix would you like to multiply by a number?\n"); + do{ + printf("Matrix A (0), B (1) or C (2)? "); + scanf("%d", &d); + }while(d < 0 || d > 2); + printf("Where would you like to store the resulting matrix?\n"); do{ printf("Matrix A (0), B (1) or C (2)? "); scanf("%d", &s); }while(s < 0 || s > 2); printf("With which number would you like to multiply matrix %s by?\n", - (s==0)?"A":(s==1)?"B":"C"); + (d==0)?"A":(d==1)?"B":"C"); scanf("%f", &n); - matrices[s] = multiplyByN(matrices[s], n); + matrices[s] = multiplyByN(matrices[d], n); break; case DIVIDE_N: - printf("What matrix would you like to fill divide by a number?\n"); + printf("What matrix would you like to fill with a number?\n"); + do{ + printf("Matrix A (0), B (1) or C (2)? "); + scanf("%d", &d); + }while(d < 0 || d > 2); + printf("Where would you like to store the resulting matrix?\n"); do{ printf("Matrix A (0), B (1) or C (2)? "); scanf("%d", &s); }while(s < 0 || s > 2); printf("With which number would you like to divide matrix %s by?\n", - (s==0)?"A":(s==1)?"B":"C"); + (d==0)?"A":(d==1)?"B":"C"); scanf("%f", &n); - matrices[s] = divideByN(matrices[s], n); + matrices[s] = divideByN(matrices[d], n); break; case RAISE_TO_N: printf("What matrix would you like to raise to the power of a number?\n"); + do{ + printf("Matrix A (0), B (1) or C (2)? "); + scanf("%d", &d); + }while(d < 0 || d > 2); + printf("Where would you like to store the resulting matrix?\n"); do{ printf("Matrix A (0), B (1) or C (2)? "); scanf("%d", &s); }while(s < 0 || s > 2); printf("With which number would you like to raise matrix %s to?\n", - (s==0)?"A":(s==1)?"B":"C"); + (d==0)?"A":(d==1)?"B":"C"); scanf("%f", &n); - matrices[s] = raiseMatrixToN(matrices[s], n); + matrices[s] = raiseMatrixToN(matrices[d], n); break; case ADD_M: diff --git a/matrix-operations.c b/matrix-operations.c index ae253f7..18c29c1 100644 --- a/matrix-operations.c +++ b/matrix-operations.c @@ -21,6 +21,7 @@ matrix_t matrixError(error_t error){ } return createMatrix(0, 0); } + // Creates a rows x columns matrix; matrix_t createMatrix(size_t rows, size_t columns){ matrix_t newmatrix; @@ -155,8 +156,11 @@ matrix_t divideMatrices(matrix_t a, matrix_t b){ matrix_t raiseMatrixToN(matrix_t m, int n){ if(!isSquare(m)) return matrixError(NOT_SQUARE); - // TODO: only accepts positive values of n matrix_t raisedmatrix = identityMatrix(m.rows); + if(n < 0){ + m = inverse(m); + n = -n; + } for(int i = 0; i < n; ++i) raisedmatrix = multiplyMatrices(raisedmatrix, m); return raisedmatrix; @@ -164,6 +168,7 @@ matrix_t raiseMatrixToN(matrix_t m, int n){ // Creates a submatrix from "matrix" without the column "column" and row "row" of "matrix" matrix_t createSubmatrix(matrix_t m, size_t row, size_t column){ + // TODO: not remove any int mod = m.rows, nmod = mod-1, sr = 0, sc = 0; matrix_t submatrix = createMatrix(nmod, nmod); for(size_t r = 0; r < mod; ++r){ @@ -239,5 +244,5 @@ matrix_t inverse(matrix_t m){ int det = determinant(m); if(det == 0) return matrixError(ZERO_DET); - return multiplyByN(adjugate(m), (float)1/det); + return divideByN(adjugate(m), det); } \ No newline at end of file