Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. //---------------------------------------------------------------------------
  5. using namespace std;
  6.  
  7. int main(int argc, char* argv[])
  8. {
  9.     int sam;
  10.         int W;
  11.     cout << "Jeśli chcesz samemu podawać liczby do macierzy wybierz 0, jeśli chcesz żeby liczby pojawiły się losowo wybierz 1" << endl;
  12.     cin>>sam;
  13.     cout << "podaj rozmiar macierzy ";
  14.     cin >> W;
  15.     float **Tab;
  16.     float **t;
  17.     int i, j, k;
  18.     Tab = new float *[W];
  19.     t = new float*[W];
  20.     for (j = 0; j<W; j++)Tab[j] = new float[j];
  21.     for (j = 0; j<W; j++)t[j] = new float[j];  
  22.    if(sam==1){
  23.     cout << "macierz pierwotna to \n\n";
  24.    for (i = 0; i<W; ++i)
  25.     {
  26.         for (j = 0; j<W; ++j)
  27.         {
  28.             Tab[i][j] = rand() % 10;
  29.             cout << Tab[i][j] << " ";
  30.         }
  31.         cout << "\n";
  32.     }
  33. }
  34. if(sam==0){
  35. for(int i = 0 ; i<W ; i++)
  36.     {
  37.         for(int j = 0 ; j<W ; j++)
  38.         {
  39.  
  40.             cout<<"Podaj liczbe która bedzie w wierszu "<< i<<  " Oraz kolumnie "<<j<< endl; ;
  41.             cin>>Tab[i][j];
  42.  
  43.         }
  44.         cout<<"\n\n";
  45.     }
  46.     cout << "macierz pierwotna to \n\n";
  47.    for (int i = 0; i<W; ++i)
  48.     {
  49.         for (int j = 0; j<W; ++j)
  50.         {
  51.             cout << Tab[i][j] << " ";
  52.         }
  53.         cout << "\n";
  54.     }
  55.     }
  56.     for (int i = 0; i<W; i++)
  57.     {
  58.         Tab[i][i] = 1 / Tab[i][i];
  59.         for (j = 0; j<W; j++) if (j != i)
  60.         {
  61.             Tab[j][i] = Tab[j][i] * Tab[i][i];
  62.             for (k = 0; k<W; k++) if (k != i)
  63.             {
  64.                 Tab[j][k] -= Tab[j][i] * Tab[i][k];
  65.                 if (j == (W - 1))
  66.                     Tab[i][k] = -(Tab[i][k] * Tab[i][i]);
  67.             }
  68.         }
  69.     }
  70.  
  71.     cout << "\nmacierz odwrotna\n\n";
  72.     int w = W - 1;
  73.     for (j = 0; j<w; j++) Tab[w][j] = -(Tab[w][j] * Tab[w][w]);
  74.  
  75.     for (int i = 0; i<W; i++)
  76.     {
  77.         for (j = 0; j<W; j++)
  78.         {
  79.             cout.width(10);
  80.             cout << Tab[i][j] << " ";
  81.         }
  82.         cout << endl;
  83.     }
  84.     cout << "\n PRESS ENTER";
  85.     cin.sync();
  86.     cin.get();
  87.     for ( int i = 0; i<W; ++i)
  88.     {
  89.         delete[] Tab[i];
  90.         delete[] t;
  91.     }
  92.     delete[] Tab;
  93.     delete[] t;
  94.     return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement