draganrakita

recursive calculation of determinant

Dec 14th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include<iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int matrica[4][4] = {   {4, 6, 1, 0},
  6. {1, 2, 7, 6},
  7. {4, 6, 1, 3},
  8. {1, 4, 7, 6}    };
  9. #define matrixSize 4
  10. //double matrica[3][3] = {  {9,8,10},
  11. //{6, 5,4},
  12. //{3,2,1}};
  13.  
  14.  
  15. double f1(int w,int x, int y, vector<int> niz,int countXfirst) {
  16.     double sum = 0;
  17.     niz.push_back(x);
  18.     if (w >= 2) {
  19.         bool jumpSum = false;
  20.         int countXNext = 0;
  21.         for (int i = 0; i < matrixSize; i++) {
  22.             jumpSum = false;
  23.             for(int j=0; j<niz.size();j++){
  24.                 if(i == niz[j]){
  25.                     jumpSum = true;
  26.                     break;
  27.                 }
  28.             }
  29.             if(!jumpSum){
  30.                 if ((countXfirst+countXNext)%2) {
  31.                     sum -= matrica[y][x] * f1(w-1,i,y+1, niz,0);
  32.  
  33.                 } else {
  34.                     sum += matrica[y][x] * f1(w-1,i,y+1, niz,0);   
  35.                 }
  36.                 countXNext++;
  37.             }
  38.         }
  39.         return sum;
  40.     } else {
  41.         return matrica[y][x];
  42.     }
  43.     return sum;
  44. }
  45.  
  46. int main() {
  47.     double det = 0;
  48.     for (int i = 0; i < matrixSize; i++){
  49.         vector<int> temp;
  50.         det +=  f1(matrixSize,i,0,temp,i);
  51.     }
  52.  
  53.     cout<<det<<endl;
  54.     system("pause");
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment