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.     double ** matrix ;
  27.     matrix = new double * [ROW]; //2D array
  28.     for (i=0; i<ROW; i++)
  29.         matrix[i] = new double[COL];
  30.        
  31.     cout << "Enter each row of the matrix separated by spaces" << endl;
  32.     for (i=0;i<ROW;i++)
  33.         for (j=0;j<COL;j++)
  34.             cin >> *(*(matrix+i)+j);
  35.     do
  36.     {
  37.            
  38.             //divide by self to get 1
  39.             double temp=*(*(matrix+row)+col);//current part to be leading entry of the column
  40.             if (temp==0)
  41.             {
  42.                //swap row part
  43.                if (row+1 == ROW_SQUARE)
  44.                   break;
  45.                else
  46.                {
  47.                swaprow(matrix,ROW,COL,row);
  48.                temp=*(*(matrix+row)+col);
  49.                print (matrix,ROW,COL);
  50.                }
  51.             }//if
  52.             if (temp!=0)
  53.             {    for (j=col;j<COL;j++)
  54.                 {
  55.                 *(*(matrix+row)+j)/=temp;
  56.                 }
  57.             print (matrix,ROW,COL);
  58.             //addmultiply next row to get 0 below the leading entry
  59.             leadingrow=row;
  60.             // this for() is to have diff process over different row
  61.             for (currentrow=leadingrow+1;currentrow<ROW_SQUARE;currentrow++)
  62.             {
  63.             temp = *(*(matrix+currentrow)+col);
  64.            
  65.             posOrNeg=1;//multiply by positive or negative
  66.             if ( posOrNeg*temp* *(*(matrix+leadingrow)+col)+temp == 0)
  67.             {
  68.                 for (j=0;j<COL;j++)
  69.                 {
  70.                 *(*(matrix+currentrow)+j)= posOrNeg*temp* (*(*(matrix+leadingrow)+j))+*(*(matrix+currentrow)+j);
  71.                 }
  72.             }
  73.             else
  74.             {
  75.                 posOrNeg=-1;
  76.                 if ( posOrNeg*temp* *(*(matrix+leadingrow)+col)+temp == 0)
  77.                 {
  78.                  
  79.                  for (j=0;j<COL;j++)
  80.                 {
  81.                 *(*(matrix+currentrow)+j)= posOrNeg*temp* (*(*(matrix+leadingrow)+j))+*(*(matrix+currentrow)+j);
  82.                 }
  83.                 }
  84.             }
  85.             print (matrix,ROW,COL);
  86.             }//if  
  87.             }//if (temp!=0)      
  88.     row++,col++;        
  89.     }while(row!=ROW_SQUARE&&col!=COL_SQUARE);
  90.     print (matrix,ROW,COL);
  91.     for (i=0;i<ROW;i++)
  92.         for (j=0;j<COL;j++)
  93.             delete [] matrix;
  94.     cin.ignore(100,\'\\n\');
  95.     cin.get();
  96.    
  97.     return 0;
  98. }
  99.  
  100. void print(double ** matrix, int ROW, int COL)
  101. {
  102.      cout << "-----------" << endl;
  103.      for (i=0;i<ROW;i++)
  104.      {   for (j=0;j<COL;j++)
  105.             cout << *(*(matrix+i)+j) << \' \';
  106.      cout << endl;}
  107. }
  108.  
  109. void checksquare(int ROW, int COL)
  110. {
  111.      if (ROW==COL)
  112.      {
  113.         ROW_SQUARE=ROW;
  114.         COL_SQUARE=COL;
  115.      }
  116.      else if (ROW<COL)
  117.      {
  118.           ROW_SQUARE=ROW;
  119.           COL_SQUARE=ROW;
  120.      }
  121.      else if (COL<ROW)
  122.      {
  123.           ROW_SQUARE=COL;
  124.           COL_SQUARE=COL;
  125.      }
  126. }
  127.  
  128. void swaprow(double ** matrix,int ROW,int COL,int row)
  129. {
  130.      float temp;
  131.      for (j=0;j<COL;j++)
  132.      {
  133.          temp=*(*(matrix+row+1)+j);
  134.          *(*(matrix+row+1)+j)=*(*(matrix+row)+j);
  135.          *(*(matrix+row)+j)=temp;
  136.      }
  137. }
');