Solving a matrix

Ok, I am just a beginning and I know there are a lot of ways to do this that are probably better, but I really wanted to make a program that converts a matrix to row-reduced echelon form. This is the central code I am using. Assume the matrix is solvable, has no leading zeroes already, etc.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cstdlib>

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}}; //Using a 
                                                                     //matrix with 
                                                                     //a known answer of 
                                                                     //{2, 4, -3}

    printmatrix(A);  //print out the matrix
    RowReduce(A); //perform the row reduced echelon form on matrix--
                           // it prints out again in the function itself
}

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 << A[i][j] << " ";
        }
        cout << endl;
    }
}

void RowReduce (float A[][4]){
  int p=3;  //rows
  int q=4;  //columns

  int lead = 0; //"lead" determines the column we are at that
                     // holds the diagonal "pivot". This helps us to zero out all numbers 
                    //above and below the pivot.

    cout << endl;
    while (lead<q-1) 
    {
        for (int i=0; i<p; i++) //for each row . . .
        {
           if (i!=lead) //When the pivot column and row are equal, 
       //we do not want to zero out the diagonal, 
       //so we skip this line and only non-"lead" rows.
                {
                    cout << A[lead][lead] << "  " << A[i][lead]; //ignore this as this
                //was part of my debugging attempts
                    for (int j=0; j<q; j++)
                    {
                        A[i][j]=A[lead][lead]*A[i][j];   // Here is where the math is and 
         //probably where the error is. I am not looking for  
        //a full identity matrix, but a fully reduced with a 
        //non-zero diagonal. Making them 1 is easy.
                        A[i][lead]=A[i][lead]*A[lead][j];
                        A[i][j]=A[i][j]-A[i][lead];
                    }
                    cout << endl;
                }
        }
    lead++;  // Now go to the next column pivot
    cout << endl;

    }
    printmatrix(A);
}



Everything gets zeroed in the right places, but the numbers are wrong. Help!
Last edited on
Put code in between syntax found here and it will make it easier to work with...
http://www.cplusplus.com/articles/z13hAqkS

Also what is
RREF.
, sorry never heard of it.
OK, I have updated everything. Thanks for the input.
Topic archived. No new replies allowed.