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