Problem p00907 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
// #define EPS 0.000000001
using namespace std;

#define EPS 0.000001

typedef vector<double> V;
typedef vector<V> MATRIX;

int d;
double ans_array[8], POW[8][6];

MATRIX gauss_jordan(MATRIX A) {

  MATRIX ret(1, V(d + 1));
  for (int col = 0; col < d + 1; col++) {
    ret[0][col] = (double)BIG_NUM;
  }

  int base_row;

  MATRIX B(d + 2, V(d + 2));
  for (int row = 0; row < d + 2; row++) {
    for (int col = 0; col < d + 2; col++) {
      B[row][col] = A[row][col];
    }
  }

  for (int i = 0; i < d + 1; i++) {

    base_row = i;

    for (int row = i + 1; row < d + 1; row++) {
      if (fabs(B[row][i]) > fabs(B[base_row][i])) {
        base_row = row;
      }
    }

    if (i != base_row) {
      swap(B[i], B[base_row]);
    }

    if (fabs(B[i][i]) < EPS)
      return ret;

    for (int col = i + 1; col < d + 2; col++) {
      B[i][col] /= B[i][i];
    }

    for (int row = 0; row < d + 1; row++) {
      if (row == i)
        continue;

      for (int col = i + 1; col < d + 2; col++) {
        B[row][col] -= B[row][i] * B[i][col];
      }
    }
  }

  double check = 0;
  for (int i = 0; i < d + 1; i++) {
    check += A[d + 1][i] * B[i][d + 1];
  }
  if (fabs(check - A[d + 1][d + 1]) > 0.1)
    return ret;

  for (int row = 0; row < d + 1; row++) {
    ret[0][row] = B[row][d + 1];
  }
  return ret;
}

void func() {

  for (int i = 0; i <= d + 2; i++) {
    scanf("%lf", &ans_array[i]);
  }

  int index, wrong_num, ans;
  double tmp;

  MATRIX ret(1, V(d + 1));
  MATRIX A(d + 2, V(d + 2));

  for (int i = 0; i <= d + 2; i++) {
    index = 0;
    for (int k = 0; k <= d + 2; k++) {
      if (k == i)
        continue;

      for (int col = 0; col < d + 1; col++) {
        A[index][col] = POW[k][col];
      }

      A[index][d + 1] = ans_array[k];
      index++;
    }

    ret = gauss_jordan(A);

    wrong_num = 0;
    for (int k = 0; k <= d + 2; k++) {
      tmp = 0;
      for (int a = 0; a <= d; a++) {
        tmp += ret[0][a] * POW[k][a];
      }

      if (fabs(tmp - ans_array[k]) > 0.1) {
        wrong_num++;
        ans = k;
      }
    }
    if (wrong_num == 1) {
      printf("%d\\n", ans);
      return;
    }
  }
}

void makePOW() {

  for (int i = 0; i < 8; i++) {
    for (int k = 0; k <= 5; k++) {
      POW[i][k] = pow(i, k);
    }
  }
}

int main() {

  makePOW();

  while (true) {
    scanf("%d", &d);
    if (d == 0)
      break;

    func();
  }

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
// #define EPS 0.000000001
using namespace std;

#define EPS 0.000001

typedef vector<double> V;
typedef vector<V> MATRIX;

int d;
double ans_array[8], POW[8][6];

MATRIX gauss_jordan(MATRIX A) {

  MATRIX ret(1, V(d + 1));
  for (int col = 0; col < d + 1; col++) {
    ret[0][col] = (double)BIG_NUM;
  }

  int base_row;

  MATRIX B;
  for (int row = 0; row < d + 2; row++) {
    for (int col = 0; col < d + 2; col++) {
      B[row][col] = A[row][col];
    }
  }

  /*printf("before\\n");
  for(int row = 0; row < d+1; row++){
          for(int col = 0; col < d+2; col++){
                  printf("%.3lf ",A[row][col]);
          }
          printf("\\n");
  }
  printf("\\n");*/

  for (int i = 0; i < d + 1; i++) { // 基準となる係数のループ

    base_row = i;

    // col == iで、一番係数の絶対値が大きいものを探す
    for (int row = i + 1; row < d + 1; row++) {
      if (fabs(B[row][i]) > fabs(B[base_row][i])) {
        base_row = row;
      }
    }

    if (i != base_row) {
      swap(B[i], B[base_row]);
    }

    if (fabs(B[i][i]) < EPS)
      return ret; // 0割エラーを防ぐ

    // まずは基準となる行の、基準となる列の係数を1にするため、基準行の全体を注目グリッドの係数で割る
    for (int col = i + 1; col < d + 2; col++) {
      B[i][col] /= B[i][i];
    }

    for (int row = 0; row < d + 1; row++) { // 他の行の、注目の列の係数を0にする
      if (row == i)
        continue;

      for (int col = i + 1; col < d + 2; col++) {
        B[row][col] -= B[row][i] * B[i][col];
      }
    }
  }

  double check = 0;
  for (int i = 0; i < d + 1; i++) {
    check += A[d + 1][i] * B[i][d + 1];
  }
  if (check > EPS)
    return ret;

  /*printf("after\\n");
          for(int row = 0; row < d+1; row++){
                  for(int col = 0; col < d+2; col++){
                          printf("%.2lf ",A[row][col]);
                  }
                  printf("\\n");
          }
          printf("\\n");*/

  for (int row = 0; row < d + 1; row++) {
    ret[0][row] = A[row][d + 1];
  }
  return ret;
}

void func() {

  for (int i = 0; i <= d + 2; i++) {
    scanf("%lf", &ans_array[i]);
  }

  int index, wrong_num, ans;
  double tmp;

  MATRIX ret(1, V(d + 1));
  MATRIX A(
      d + 2,
      V(d +
        2)); // ★★未知数の数はd+1個なので(0乗[定数]～d乗)、式はd+1個で良いはず(d+1列は解格納用配列)

  for (int i = 0; i <= d + 2; i++) { // 1つ目のSKIPする解のループ
    index = 0;
    for (int k = 0; k <= d + 2; k++) {
      if (k == i)
        continue;
      // 係数をはめ込む
      for (int col = 0; col < d + 1; col++) {
        A[index][col] = POW[k][col];
      }

      A[index][d + 1] =
          ans_array[k]; // 行列の第d+1列に解を縦に並べる(掃き出し法)
      index++;
    }

    ret = gauss_jordan(A);

    /*printf("\\n");
    for(int k = 0; k < d+1;k++){
            printf("ret[%d]:%.3lf\\n",k,ret[0][k]);
    }*/

    wrong_num = 0;
    for (int k = 0; k <= d + 2;
         k++) { // 実際に係数に数字のべき乗をかけて解と合うか確かめる
      tmp = 0;
      for (int a = 0; a <= d; a++) {
        tmp += ret[0][a] * POW[k][a];
      }

      // printf("tmp:%.3lf ans:%.3lf\\n",tmp,ans_array[k]);

      if (fabs(tmp - ans_array[k]) > 0.1) {
        wrong_num++;
        ans = k;
      }
    }
    if (wrong_num == 1) {
      printf("%d\\n", ans);
      return;
    }
  }
}

void makePOW() {

  for (int i = 0; i < 8; i++) {
    for (int k = 0; k <= 5; k++) {
      POW[i][k] = pow(i, k);
    }
  }
}

int main() {

  makePOW();

  while (true) {
    scanf("%d", &d);
    if (d == 0)
      break;

    func();
  }

  return 0;
}