Simple matrix solution in below line in C ++

Okay, I'm pulling all my hair out on this one, although as a noob I'm sure there are a few issues. I want to take a matrix and, chanting elementary row operations, reduced it to an echelon form reduced by rows. Suppose that (1) it is solvable and (2) the only solution. There is no check for zeros or anything else; it just does the operations on the line. Here is the code:

#include <iostream>
#include <cstdlib>

using namespace std;

void printmatrix(float A[][4]);
void RowReduce (float A[][4]);

int main() {

    // answer should be { 2, 4, -3 }
    float A[3][4] = {
        { 5, -6, -7,   7 },
        { 3, -2,  5, -17 },
        { 2,  4, -3,  29 }
    };

    printmatrix(A);
    RowReduce(A);

}

// Outputs the matrix
void printmatrix(float A[][4]) { 

    int p = 3;
    int q = 4;

    for (int i = 0; i < p; i++) {
        for (int j = 0; j < q; j++) {
            cout << A[i][j] << " ";
        }
        cout << endl;
    } 

}

void RowReduce (float A[][4]){

    //rows
    int p = 3;  
    //columns
    int q = 4;  

    // the determines the column we are at which holds the diagonal,
    // the basis for all elimination above and below
    int lead = 0; 

    cout << endl;
    while ( lead < q - 1 ) {

        // for each row . . .
        for (int i = 0; i < p; i++)  {

            // ignore the diagonal, and we will not have a tree rref
            // as the diagonal will not be divided by itself. I can fix that.
            if ( i != lead )  {

                cout << A[lead][lead] << "  " << A[i][lead];

                for (int j = 0; j < q; j++) {

                    //here is the math . . . . probably where the problem is?
                    A[i][j]    = A[lead][lead] * A[i][j]; 
                    A[i][lead] = A[i][lead]    * A[lead][j];
                    A[i][j]    = A[i][j]       - A[i][lead];

                }

                cout << endl;

            }
        }

        // now go to the next pivot
        lead++;  
        cout << endl;

    }

}

      

I tried to do it manually, but what I get is of course the correct answer, but this is getting a diagonal matrix - that's great - but wrong answer!

+3


source to share


1 answer


The main mistake in your code is when you calculate the divisor or multiplier in a for loop. You have to calculate them before iterating over the cells.

Hint: Debugging is easier if the code is well-formed and the variables have meaningful names.

See the implementation RowReduce()

:



#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

void printmatrix(float A[][4]);
void RowReduce(float A[][4]);

int main()
{
    float A[3][4] = {{5, -6, -7,   7},
                     {3, -2,  5, -17},
                     {2,  4, -3,  29}}; //answer should be {2, 4, -3}

    printmatrix(A);
    RowReduce(A);
}

void printmatrix(float A[][4]) // Outputs the matrix
{
    int p=3;
    int q=4;

    for (int i=0; i<p; i++) {
            for (int j=0; j<q; j++) {
                    cout << setw(7) << setprecision(4) << A[i][j] << " ";
            }
            cout << endl;
    }

    cout << endl;
}

void RowReduce(float A[][4])
{
    const int nrows = 3; // number of rows
    const int ncols = 4; // number of columns

    int lead = 0; 

    while (lead < nrows) {
        float d, m;

        for (int r = 0; r < nrows; r++) { // for each row ...
            /* calculate divisor and multiplier */
            d = A[lead][lead];
            m = A[r][lead] / A[lead][lead];

            for (int c = 0; c < ncols; c++) { // for each column ...
                if (r == lead)
                    A[r][c] /= d;               // make pivot = 1
                else
                    A[r][c] -= A[lead][c] * m;  // make other = 0
            }
        }

        lead++;
        printmatrix(A);
    }
}

      

Output:

  5      -6      -7       7 
  3      -2       5     -17 
  2       4      -3      29 


  1    -1.2    -1.4     1.4 
  0     1.6     9.2   -21.2 
  0     6.4    -0.2    26.2 

  1       0     5.5   -14.5 
  0       1    5.75  -13.25 
  0       0     -37     111 

  1       0       0       2 
  0       1       0       4 
  0       0       1      -3

      

+4


source







All Articles