Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define DIMENSIONE 50
- double *backSub(double mat[DIMENSIONE][DIMENSIONE + 1], int grado) {
- double *soluzioni = new double[grado];
- // Sostituiamo partendo da x(n) = k
- for (int i = grado - 1; i >= 0; i--) {
- soluzioni[i] = mat[i][grado];
- // Assumiamo una triangolare superiore
- for (int j = i + 1; j < grado; j++) {
- soluzioni[i] -= mat[i][j] * soluzioni[j];
- }
- // Divisione per i coefficienti
- soluzioni[i] = soluzioni[i] / mat[i][i];
- }
- return soluzioni;
- }
- int main() {
- using namespace std;
- // Dichiriamo una DIMENSIONE x DIMENSIONE
- double M[DIMENSIONE][DIMENSIONE + 1];
- int grado;
- cout << "Inserire il grado del sistema: ";
- cin >> grado;
- if (grado > DIMENSIONE - 1) {
- cout << "La dimensione della matrice e' troppo grande per essere calcolata"
- << endl;
- return 0;
- }
- // La carichiamo
- for (int i = 0; i < grado; i++) {
- for (int j = 0; j < grado + 1; j++) {
- cout << "Agg(" << i + 1 << "," << j + 1 << "): ";
- cin >> M[i][j];
- }
- }
- double *soluzioni = backSub(M, grado);
- cout << "Vettore delle soluzioni: " << endl;
- for (int i = 0; i < grado; i++) {
- cout << "X" << i + 1 << ": " << soluzioni[i] << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment