document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2. rowreduction_myversion.cpp
  3. 12/12/2010
  4. 1. if the current elemet of matrix[temp] have value zero, swap
  5. 2. divide by itself -> 1 -> leading entries
  6. 3. add multiply all next rows to make every element under leading entries as 0
  7. the result of reduced matrix will be stored in the same matrix. print for each step.
  8. Last matrix is the result
  9. */
  10. #include <iostream>
  11. using namespace std;
  12. int i,j, ROW_SQUARE, COL_SQUARE;
  13.  
  14. void print(double ** matrix, int ROW, int COL);
  15. void checksquare(int ROW, int COL);
  16. void swaprow(double ** matrix,int ROW,int COL,int row);
  17.  
  18. int main()
  19. {
  20.     int ROW,COL,row=0,col=0,leadingrow, currentrow,posOrNeg;
  21.     cout << "Enter height: ";
  22.     cin >> ROW;
  23.     cout << "Enter column: ";
  24.     cin >> COL;
  25.     checksquare(ROW,COL);
  26.    
  27.     double ** matrix ;
  28.     matrix = new double * [ROW]; //2D array
  29.     for (i=0; i<ROW; i++)
  30.         matrix[i] = new double[COL];
  31.        
  32.     cout << "Enter each row of the matrix separated by spaces" << endl;
  33.     for (i=0;i<ROW;i++)
  34.         for (j=0;j<COL;j++)
  35.             cin >> *(*(matrix+i)+j);
  36.     do
  37.     {
  38.            
  39.             //divide by self to get 1
  40.             double temp=*(*(matrix+row)+col);//current part to be leading entry of the column
  41.             if (temp==0)
  42.             {
  43.                //swap row part
  44.                if (row+1 == ROW_SQUARE)
  45.                   break;
  46.                else
  47.                {
  48.                swaprow(matrix,ROW,COL,row);
  49.                temp=*(*(matrix+row)+col);
  50.                print (matrix,ROW,COL);
  51.                }
  52.             }//if
  53.             if (temp!=0)
  54.             {    for (j=col;j<COL;j++)
  55.                 {
  56.                 *(*(matrix+row)+j)/=temp;
  57.                 }
  58.             print (matrix,ROW,COL);
  59.             //addmultiply next row to get 0 below the leading entry
  60.             leadingrow=row;
  61.             // this for() is to have diff process over different row
  62.             for (currentrow=leadingrow+1;currentrow<ROW_SQUARE;currentrow++)
  63.             {
  64.             temp = *(*(matrix+currentrow)+col);
  65.            
  66.             posOrNeg=1;//multiply by positive or negative
  67.             if ( posOrNeg*temp* *(*(matrix+leadingrow)+col)+temp == 0)
  68.             {
  69.                 for (j=0;j<COL;j++)
  70.                 {
  71.                 *(*(matrix+currentrow)+j)= posOrNeg*temp* (*(*(matrix+leadingrow)+j))+*(*(matrix+currentrow)+j);
  72.                 }
  73.             }
  74.             else
  75.             {
  76.                 posOrNeg=-1;
  77.                 if ( posOrNeg*temp* *(*(matrix+leadingrow)+col)+temp == 0)
  78.                 {
  79.                  
  80.                  for (j=0;j<COL;j++)
  81.                 {
  82.                 *(*(matrix+currentrow)+j)= posOrNeg*temp* (*(*(matrix+leadingrow)+j))+*(*(matrix+currentrow)+j);
  83.                 }
  84.                 }
  85.             }
  86.             print (matrix,ROW,COL);
  87.             }//if  
  88.             }//if (temp!=0)      
  89.     row++,col++;        
  90.     }while(row!=ROW_SQUARE&&col!=COL_SQUARE);
  91.     print (matrix,ROW,COL);
  92.    
  93.     for (i=0;i<ROW;i++)
  94.             delete [] matrix[i];
  95.     delete[] matrix;
  96.     matrix = NULL;
  97.    
  98.     cin.ignore(100,\'\\n\');
  99.     cin.get();
  100.    
  101.     return 0;
  102. }
  103.  
  104. void print(double ** matrix, int ROW, int COL)
  105. {
  106.      cout << "-----------" << endl;
  107.      for (i=0;i<ROW;i++)
  108.      {   for (j=0;j<COL;j++)
  109.             cout << *(*(matrix+i)+j) << \' \';
  110.      cout << endl;}
  111. }
  112.  
  113. void checksquare(int ROW, int COL)
  114. {
  115.      if (ROW==COL)
  116.      {
  117.         ROW_SQUARE=ROW;
  118.         COL_SQUARE=COL;
  119.      }
  120.      else if (ROW<COL)
  121.      {
  122.           ROW_SQUARE=ROW;
  123.           COL_SQUARE=ROW;
  124.      }
  125.      else if (COL<ROW)
  126.      {
  127.           ROW_SQUARE=COL;
  128.           COL_SQUARE=COL;
  129.      }
  130. }
  131.  
  132. void swaprow(double ** matrix,int ROW,int COL,int row)
  133. {
  134.      double temp;
  135.      for (j=0;j<COL;j++)
  136.      {
  137.          temp=*(*(matrix+row+1)+j);
  138.          *(*(matrix+row+1)+j)=*(*(matrix+row)+j);
  139.          *(*(matrix+row)+j)=temp;
  140.      }
  141. }
');