[ create a new paste ] login | about

Link: http://codepad.org/E061iAYs    [ raw code | output | fork ]

C++, pasted on May 23:
#include <iostream>
using namespace std;
const int N = 3;
void calc(double a[][N + 1]) {
  int k, i, j;
  double c;

  for (k = 0; k < N; k++) {
    c = a[k][k];
    for (i = 0; i < N + 1; i++)
      a[k][i] /= c;
    for (i = k + 1; i < N; i++) {
      c = a[i][k];
      for (j = k; j < N + 1; j++)
        a[i][j] /= c;
    }
    for (i = k + 1; i < N; i++)
      for (j = k; j < N + 1; j++)
        a[i][j] -= a[k][j];
  }
  for (k = N - 1; k >= 0; --k) {
    c = a[k][N];
    for (i = k - 1; i >= 0; --i) {
      a[i][N] -= c * a[i][k];
/*      a[i][k] = 0.0; */
    }
  }
}
int main() {
  static double a[N][N + 1] = {
    { 3.0, 1.0, 1.0, 10.0 },
    { 1.0, 5.0, 2.0, 21.0 },
    { 1.0, 2.0, 5.0, 30.0 }
  };
  calc(a);
  cout << "x = " << a[0][N] << endl;
  cout << "y = " << a[1][N] << endl;
  cout << "z = " << a[2][N] << endl;
  return 0;
}
/* end */


Output:
1
2
3
x = 1
y = 2
z = 5


Create a new paste based on this one


Comments: