Guys I have an assignment on programming that needs to implement the eigenvalues algorithm in C. The maximum MATRIX is 5X5 and the minimum is 2X2.
My code already checks the upper half of the matrix (X) and the Diagonalization Values (O). I left with the lower right of the matrix (Y).
O X X X
X O X Y
X X O Y
X Y Y O
Here is what I got so far.
Code:#include <math.h> #include <stdio.h> #include <stdlib.h> int main(void) { int num, col, row, i, j, sum = 0, p = 1; char stream[256]; // correct input from the user printf("Enter the dimension of your matrix [2-5]: "); do { char *line = fgets(stream, 256, stdin); char c; if (sscanf(line, " %d %c", &num, &c) == 1) { system("cls"); if (num > 5) printf("Dimension too big!\nRetry [2-5]: "); else if (num < 2) printf("Dimension too small!\nRetry [2-5]: "); } else { system("cls"); printf("Invalid input!\nEnter the dimension of your matrix [2-5]: "); } } while (num > 5 || num < 2); // create the matrix based on the size of input int matrix[num][num]; system("cls"); printf("Enter the matrix:\n\n"); for (i = 0; i < num; i++) for (col = 0; col < num; col++) scanf("%d", &matrix[i][col]); // print out the diagonalized values printf("\nDiagonalization Values: "); for (i = 0; i < num; i++) printf("%d ", matrix[i][i]); // check the upper-left half of the matrix printf("\nThe constant is: "); for (i = 0; i < num; i++) { int cnt = 0, p = 1; if (i < num) for (row = 0; row < num; row++) { col = (num - 1) - row; if (row != col - i && col - i >= 0) { p *= matrix[row][col - i]; cnt++; } } if (cnt != 0) sum += p; } // check the lower-right half of the matrix for (j = 1; j < num - 1; j++) { int cnt = 0, p = 1; if (j < num - 1) for (row = 1, col = (num - 1); row < num; row++, col--) { if (row != col) { p *= matrix[row][col]; cnt++; } } if (cnt != 0) sum += p; } printf("\n\n%d", sum); return 0; }



LinkBack URL
About LinkBacks



