Row Reduction/Gaussian Elimination

A bit problem with one of tested output.

Page 1 of 1

5 Replies - 2102 Views - Last Post: 14 December 2010 - 04:03 PM Rate Topic: -----

#1 shad0wk1  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 139
  • Joined: 01-October 10

Row Reduction/Gaussian Elimination

Posted 14 December 2010 - 05:27 AM

There was a thread about row reduction some time ago. I was interested because I haven't tried it yet. So this is my try on it. target is the reduced form. There is no compiler error, but maybe a logic error.

I check my program using 3 matrix inputs. The third input didn't give the expected output. Here is the list of intended output according to its input. Would you be kind to help me by pointing out why?

Edit: Some explanation: It ask user to input the height and column of the matrix. Check for the closest square size[According to theory we only need to put the square size part as eicholon]. Then, ask user input for each lines of the matrix, with each column separated by space. Start row reduction. Save the element in matrix[a][a] (a is used to say it is same value, that is, the square size.) as temp. If it is zero, I check if it is the last row, TRUE exit loop, else, swap row with next row. Then I divide the row with temp making the leading entry (1). Then add-multiply every next row to get 0 below the leading entry. This is by theory, if the previous row has been done, I have no need to look at in anymore. Add-multiply may be positive or negative(posOrNeg), so I check both for which one result in zero. Loop end when reach square size. Print matrix for each step, and print last changed matrix as the result.

Input 1:
3 2 1
4 5 6

Input 2:
1 2 3
4 5 6
7 8 9

Input 3:
0 1 2
3 4 5
6 7 8

Output 1:
1 0.666667 0.333333
0 1 2

Output 2:
1 2 3
0 1 2
0 0 0

Output 3:
0 1.333333 1.666667
0 1 2
0 0 0


Output 3[given by my code]:
0 1.333333 1.666667
0 1 2
0 0 1


My observation
It have different result when changing from 
1 1.33333 1.666667
0 1 2
0 -1 -2


The program code:
/*rowreduction_myversion.cpp
12/12/2010
1. if the current elemet of matrix[temp] have value zero, swap
2. divide by itself -> 1 -> leading entries
3. add multiply all next rows to make every element under leading entries as 0
the result of reduced matrix will be stored in the same matrix. print for each step.
Last matrix is the result*/
#include <iostream>
using namespace std;
int i,j, ROW_SQUARE, COL_SQUARE;

void print(double ** matrix, int ROW, int COL);
void checksquare(int ROW, int COL);
void swaprow(double ** matrix,int ROW,int COL,int row);

int main()
{
    int ROW,COL,row=0,col=0,leadingrow, currentrow,posOrNeg;
    cout << "Enter height: ";
    cin >> ROW;
    cout << "Enter column: ";
    cin >> COL;
    checksquare(ROW,COL);
    
    double ** matrix ;
    matrix = new double * [ROW]; //2D array
    for (i=0; i<ROW; i++)
        matrix[i] = new double[COL];
        
    cout << "Enter each row of the matrix separated by spaces" << endl;
    for (i=0;i<ROW;i++)
        for (j=0;j<COL;j++)
            cin >> *(*(matrix+i)+j);
    do
    {
            
            //divide by self to get 1
            double temp=*(*(matrix+row)+col);//current part to be leading entry of the column
            if (temp==0)
            {
               //swap row part
               if (row+1 == ROW_SQUARE)
                  break;
               else
               {
               swaprow(matrix,ROW,COL,row);
               temp=*(*(matrix+row)+col);
               print (matrix,ROW,COL);
               }
            }//if
            if (temp!=0)
            {    for (j=col;j<COL;j++)
                {
                *(*(matrix+row)+j)/=temp;
                }
            print (matrix,ROW,COL);
            //addmultiply next row to get 0 below the leading entry
            leadingrow=row;
            // this for() is to have diff process over different row
            for (currentrow=leadingrow+1;currentrow<ROW_SQUARE;currentrow++)
            {
            temp = *(*(matrix+currentrow)+col);
            
            posOrNeg=1;//multiply by positive or negative
            if ( posOrNeg*temp* *(*(matrix+leadingrow)+col)+temp == 0)
            {
                for (j=0;j<COL;j++)
                {
                *(*(matrix+currentrow)+j)= posOrNeg*temp* (*(*(matrix+leadingrow)+j))+*(*(matrix+currentrow)+j);
                } 
            }
            else
            {
                posOrNeg=-1;
                if ( posOrNeg*temp* *(*(matrix+leadingrow)+col)+temp == 0)
                {
                 
                 for (j=0;j<COL;j++)
                {
                *(*(matrix+currentrow)+j)= posOrNeg*temp* (*(*(matrix+leadingrow)+j))+*(*(matrix+currentrow)+j);
                }
                } 
            }
            print (matrix,ROW,COL);
            }//if  
            }//if (temp!=0)      
    row++,col++;        
    }while(row!=ROW_SQUARE&&col!=COL_SQUARE);
    print (matrix,ROW,COL);
    
    for (i=0;i<ROW;i++)
            delete [] matrix[i];
    delete[] matrix;
    matrix = NULL;
    
    cin.ignore(100,'\n');
    cin.get();
    
    return 0;
}//end main()

void print(double ** matrix, int ROW, int COL)
{
     cout << "-----------" << endl;
     for (i=0;i<ROW;i++)
     {   for (j=0;j<COL;j++)
            cout << *(*(matrix+i)+j) << ' ';
     cout << endl;}
}

void checksquare(int ROW, int COL)
{
     if (ROW==COL)
     {
        ROW_SQUARE=ROW;
        COL_SQUARE=COL;
     }
     else if (ROW<COL)
     {
          ROW_SQUARE=ROW;
          COL_SQUARE=ROW;
     }
     else if (COL<ROW)
     {
          ROW_SQUARE=COL;
          COL_SQUARE=COL;
     }
}

void swaprow(double ** matrix,int ROW,int COL,int row)
{
     float temp;
     for (j=0;j<COL;j++)
     {
         temp=*(*(matrix+row+1)+j);
         *(*(matrix+row+1)+j)=*(*(matrix+row)+j);
         *(*(matrix+row)+j)=temp;
     }
}


This post has been edited by shad0wk1: 14 December 2010 - 06:01 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Row Reduction/Gaussian Elimination

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Row Reduction/Gaussian Elimination

Posted 14 December 2010 - 09:36 AM

I haven't actually tried your code but when I compiled the code I got these warnings.

Quote

main.cpp||In function ‘int main()’:|
main.cpp|39|warning: comparing floating point with == or != is unsafe|
main.cpp|51|warning: comparing floating point with == or != is unsafe|
main.cpp|65|warning: comparing floating point with == or != is unsafe|
main.cpp|75|warning: comparing floating point with == or != is unsafe|
main.cpp|130|warning: unused parameter ‘ROW’|
||=== Build finished: 0 errors, 5 warnings ===|


You might want to see if these warnings are causing some of your problems.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: Row Reduction/Gaussian Elimination

Posted 14 December 2010 - 10:00 AM

> temp=*(*(matrix+row+1)+j);
1. temp is a float, but the arrays are double, so you're losing information.
2. What's wrong with matrix[row][j] notation?
Was This Post Helpful? 1
  • +
  • -

#4 shad0wk1  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 139
  • Joined: 01-October 10

Re: Row Reduction/Gaussian Elimination

Posted 14 December 2010 - 03:22 PM

Ah, I forgot to change float to double at the function spawrow(). Simple mistake I made. I thought I changed every float already. It's working now after I change temp to double. Thank you for every help.

Jim, I didn't get that error. I am using DevC++ IDE. Can help me on how to increase the warning level?

Salem_c, I didn't use matrix[row][j] notation because that is easy. I am trying to challenge myself and review stuffs that I am not good at. Among them are the pointer notation.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Row Reduction/Gaussian Elimination

Posted 14 December 2010 - 03:52 PM

I am not familiar with DevC++. But you might want to think about changing compilers as Dev-C++ has not been updated in quite a few years. See this recent thread IDE take note of post #5.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 shad0wk1  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 139
  • Joined: 01-October 10

Re: Row Reduction/Gaussian Elimination

Posted 14 December 2010 - 04:03 PM

I was going to put "changing compiler" in my future schedule. Oh well, I'll check it out. I have Eclipse and the other wxdevc++ in my mind now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1