0
 for(count = 0; count < max; count ++)
 {
    for (row=(count+1); row < max; row++)
    {
        for(column = 0; column < max; column ++)
        {
            double t = matrix[row][count]/matrix[count][count];
            matrix[row][column] = (matrix[row][column] - (t*matrix[count][column]));
        }
    }
  }

This is my code for the Gaussian elimination process, all variable were declared previously. The original matrix is:

1.4 2.1 2.1 7.4 9.6

1.6 1.5 1.1 0.7 5.0

3.8 8.0 9.6 5.4 8.8

4.6 8.2 8.4 0.4 8.0

2.6 2.9 0.1 9.6 7.7

The output I am getting is:

I perform a row swap so the matrix now looks like:

4.6 8.2 8.4 0.4 8

3.8 8 9.6 5.4 8.8

1.4 2.1 2.1 7.4 9.6

2.6 2.9 0.1 9.6 7.7

1.6 1.5 1.1 0.7 5

The output I am getting is:

4.6 8.2 8.4 0.4 8

0 8 9.6 5.4 8.8

0 0 2.1 7.4 9.6

0 0 0 9.6 7.7

0 0 0 0 5

As can be seen, the process is working partially, eliminating the leading terms in each row. However, it is not subtracting the the remaining terms. e.g. matrix[1][1] in the original matrix, after the row swap = 8 and matrix[1][1] after gaussian elimination is still 8.

I was wondering if anyone would know what my problem is as i cannot find it.

  • *I was wondering if anyone would know what my problem is as i cannot find it.* -- Have you used the debugger that comes with your compiler? – PaulMcKenzie Dec 16 '15 at 00:39
  • 1
    Your computation of `t` is inside the column loop but it only works if before the column loop. It isn't just an efficiency problem because you modify the inputs to `t` when you don't want `t` to change. – JSF Dec 16 '15 at 00:49
  • @PaulMcKenzie No I haven't, there is not any computing errors with my code, it's just not executing as I expected – user4878925 Dec 16 '15 at 00:49
  • @PaulMcKenzie Thanks, Paul that was it, easy fix as it turns out. – user4878925 Dec 16 '15 at 00:50
  • @user4878925 I guess you don't know what a debugger is. It is a tool that you use to run your program a single step at a time. You can watch variables, set breakpoints, and see the flow of your program. Now would be the best time to learn to use it. Every programmer makes logical mistakes with their code, and the debugger is the tool they use to see where the logic goes awry. Believe it or not, we don't run the program "in our minds", unless it is so trivial. – PaulMcKenzie Dec 16 '15 at 00:51
  • @user4878925 Try using a **debugger** if you have available one. The code compiles, so you will be able to use one if it is available. Alternative way: add code to print values on thf way and examine what is happening. – MikeCAT Dec 16 '15 at 00:52
  • You say "The output I am getting is: I perform a row swap " however you did not post any code for a row swap. Please post the output of the *actual* code you posted. – M.M Dec 16 '15 at 00:58

1 Answers1

3

This line:

double t = matrix[row][count]/matrix[count][count];

should be before the line:

for(column = 0; column < max; column ++)

The problem is that after the first iteration of the column loop you set matrix[1][0] = 0;. But then for the second iteration of the column loop, you compute t using matrix[1][0]/matrix[0][0], but that is now 0 because you just set matrix[1][0] to 0.

NB. To help with debugging I added a printf output to see exactly what calculation was happening. This is primitive compared to using a debugger but it is a useful tool to have in your belt.

M.M
  • 138,810
  • 21
  • 208
  • 365