Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4. typedef float matrice[101][101];
  5. int scrivinum();
  6. void scrivimatr(matrice, int, char);
  7. void stampamatr(matrice, int);
  8. void scalini(matrice, int);
  9. int indpivot(matrice, int, int);
  10. float valpivot(matrice, int, int);
  11. void scambiarighe(matrice, int, int, int);
  12. bool sistemacolonne(matrice, int, int, int);
  13. void sottraimultiploriga(matrice, int, int, int, float);
  14. void annullacoeff(matrice, int);
  15. void ridotta(matrice, int);
  16. void pulisci(matrice, int);
  17.  
  18. int main() {
  19.     matrice x;
  20.     int n = scrivinum();
  21.     scrivimatr(x, n, 'x');
  22.     cout << endl << endl << endl;
  23.     stampamatr(x, n);
  24.     scalini(x, n);
  25.     ridotta(x, n);
  26.     cout << endl;
  27.     stampamatr(x, n);
  28.     system("PAUSE");
  29.     return 0;
  30. }
  31.  
  32.  
  33. int scrivinum() {
  34.     int a;
  35.     do {
  36.         cout << "Inserisci un numero compreso tra 1 e 100: ";
  37.         cin >> a;
  38.         cout << endl;
  39.     } while ((a > 100) || (a < 0));
  40.     return a;
  41. }
  42.  
  43. void scrivimatr(matrice matr, int n, char s) {
  44.     for (int i = 1; i <= n; i++) {
  45.         cout << "inserisci la riga " << i << " di " << s << ":";
  46.         for (int j = 1; j <= n; j++) {
  47.             cout << endl;
  48.             cin >> matr[i][j];
  49.         }
  50.         cout << endl;
  51.     }
  52.     cout << endl;
  53.     return;
  54. }
  55.  
  56. void stampamatr(matrice matr, int n) {
  57.     cout << endl << endl;
  58.     for (int i = 1; i <= n; i++) {
  59.         for (int j = 1; j <= n; j++) {
  60.             cout << matr[i][j] << " ";
  61.         }
  62.         cout << endl;
  63.     }
  64.     cout << endl << endl;
  65.     return;
  66. }
  67.  
  68.  
  69. bool sistemacolonne(matrice matr, int n, int colonna, int innesco) {
  70.     bool m = true;
  71.     int i = innesco;
  72.     while ((i <= n) && m) {
  73.         if (matr[i][colonna] != 0) {
  74.             scambiarighe(matr, n, innesco, i);
  75.             if (i != innesco) {
  76.                 cout << "\n\n" << "Scambiate le righe " << innesco << " e " << i << endl;
  77.                 stampamatr(matr, n);
  78.             }
  79.             m = false;
  80.         }
  81.         i++;
  82.     }
  83.     return !m;
  84. }
  85.  
  86. void scalini(matrice matr, int n) {
  87.     float fatt;
  88.     int pivot = 1;
  89.     for (int i = 1; i <= n; i++) {//per ogni colonna
  90.         bool mark = sistemacolonne(matr, n, i, pivot);//mette un non-0 come primo elemento della riga i, se può, partendo da tante righe quanti pivot ha trovato
  91.         cout << endl;
  92.         if (mark) {  // se c'è un elemento non nullo nella colonna
  93.             for (int j = pivot + 1; j <= n; j++) {//per ogni riga sotto quella di pivot
  94.                 fatt = matr[j][i] / matr[pivot][i];
  95.                 sottraimultiploriga(matr, n, j, pivot, fatt); //sottrae la riga di pivot moltiplicata per il giusto fattore
  96.             }
  97.             pivot++; //se si è trovato un pivot, segnalalo
  98.         }
  99.     }
  100.     return;
  101. }
  102.  
  103. int indpivot(matrice matr, int n, int riga) {
  104.     int o = 0;
  105.     int i = 1;
  106.     while ((i <= n) && (o == 0)) {
  107.         if (matr[riga][i] != 0) o = i;
  108.         i++;
  109.     }
  110.     return o;
  111. }
  112.  
  113. float valpivot(matrice matr, int n, int riga) {
  114.     int i = 1;
  115.     float pivot = 0;
  116.     while ((i <= n) && (pivot == 0)) {
  117.         if (matr[riga][i] != 0) pivot = matr[riga][i];
  118.         i++;
  119.     }
  120.     return pivot;
  121. }
  122.  
  123.  
  124. void scambiarighe(matrice matr, int n, int riga1, int riga2) {
  125.     float appo;
  126.     for (int i = 1; i <= n; i++) {
  127.         appo = matr[riga1][i];
  128.         matr[riga1][i] = matr[riga2][i];
  129.         matr[riga2][i] = appo;
  130.     }
  131.     return;
  132. }
  133.  
  134. void sottraimultiploriga(matrice matr, int n, int sottratta, int toglie, float fatt) {
  135.     for (int i = 1; i <= n; i++) matr[sottratta][i] = matr[sottratta][i] - (matr[toglie][i] * fatt);
  136.     if ((fatt != 0) && (sottratta != toglie)) {
  137.         cout << "Sottratta alla riga " << sottratta << " la riga " << toglie << " moltiplicata per " << fatt;
  138.         stampamatr(matr, n);
  139.     }
  140.     return;
  141. }
  142.  
  143. void annullacoeff(matrice matr, int n) {
  144.     float fatt;
  145.     for (int i = 1; i <= n; i++) {
  146.         if ((valpivot(matr, n, i)) != 0) {
  147.             fatt = valpivot(matr, n, i);
  148.             for (int j = 1; j <= n; j++) matr[i][j] = matr[i][j] / fatt;
  149.             if (fatt != 1) {
  150.                 cout << "Moltiplicata la riga " << i << " per " << fatt;
  151.                 stampamatr(matr, n);
  152.             }
  153.         }
  154.     }
  155.     return;
  156. }
  157.  
  158. void ridotta(matrice matr, int n) {
  159.     annullacoeff(matr, n);
  160.     float fatt;
  161.     int ind;
  162.     for (int i = n; i > 1; i--) {
  163.         if (indpivot(matr, n, i) != 0) {
  164.             ind = indpivot(matr, n, i);
  165.             for (int j = i - 1; j >= 1; j--) {
  166.                 fatt = matr[j][ind];
  167.                 sottraimultiploriga(matr, n, j, i, fatt);
  168.             }
  169.         }
  170.     }
  171.     pulisci(matr, n);
  172.     return;
  173. }
  174.  
  175. void pulisci(matrice matr, int n) {
  176.     for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (fabs(matr[i][j]) < 0.00000001) matr[i][j] = 0;
  177.     return;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement