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

#include <math.h>
#include <stdio.h>
#include <string.h>
int main(void) {
  int a, s[41], d[41][22], f[41], g, h[41], i, j, z, x;
  while (1) {
    scanf("%d", &a);
    if (a == 0)
      break;
    for (i = 0; i < a; i++) {
      scanf("%d", &s[i]);
      f[i] = 0;
      g = 1;
      h[i] = 0;
      while (1) {
        scanf("%d", &d[i][f[i]]);
        h[i] += d[i][f[i]];
        if (d[i][f[i]] == 10 && g % 2 == 1) {
          g++;
        }
        g++;
        f[i]++;
        if (g == 19)
          break;
      }
      scanf("%d %d", &d[i][f[i]], &d[i][f[i] + 1]);
      h[i] += d[i][f[i]] + d[i][f[i] + 1];
      if (d[i][f[i]] + d[i][f[i] + 1] >= 10) {
        scanf("%d", &d[i][f[i] + 2]);
        h[i] += d[i][f[i] + 2];
      }
      z = 0;
      for (j = 0; j < f[i]; j++) {
        if (d[i][j] == 10 && z == 0) {
          h[i] += d[i][j + 1] + d[i][j + 2];
        } else if (z == 0)
          z = 1;
        else if (d[i][j - 1] + d[i][j] == 10 && z == 1) {
          h[i] += d[i][j + 1];
          z = 0;
        } else if (z == 1)
          z = 0;
      }
    }
    x = 0;
    for (i = 0; i < a - 1; i++) {
      for (j = i + 1; j < a; j++) {
        if (h[i] < h[j]) {
          x = h[i];
          h[i] = h[j];
          h[j] = x;
          x = s[i];
          s[i] = s[j];
          s[j] = x;
        }
        if (h[i] == h[j]) {
          if (s[i] > s[j]) {
            x = h[i];
            h[i] = h[j];
            h[j] = x;
            x = s[i];
            s[i] = s[j];
            s[j] = x;
          }
        }
      }
    }
    for (i = 0; i < a; i++)
      printf("%d %d\\n", s[i], h[i]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <string.h>
int i, j, k;
int a, s[100], d[100][30], f, g[100], h, z[100][30], x[100][30], c, q[100];
int aa[100][30], ss;
int main(void) {
  while (1) {
    f = 0;
    h = 0;
    c = 0;
    ss = 0;
    h = 0;
    scanf("%d", &a);
    if (a == 0)
      break;
    for (i = 0; i < 99; i++) {
      s[i] = 0;
      g[i] = 0;
      q[i] = 0;
      for (j = 0; j < 21; j++) {
        d[i][j] = 0;
        aa[i][j] = 0;
        z[i][j] = 0;
        x[i][j] = 0;
      }
    }
    for (i = 0; i < a; i++) {
      f = 18;
      g[i] = 0;
      scanf("%d", &s[i]);
      while (1) {
        scanf("%d", &d[i][g[i]]);
        if (d[i][g[i]] == 10)
          f--;
        g[i]++;
        f--;
        if (f == 0)
          break;
      }
      scanf("%d", &d[i][g[i]]);
      scanf("%d", &d[i][g[i] + 1]);
      if (d[i][g[i]] == 10 || d[i][g[i] + 1] == 10 ||
          d[i][g[i]] + d[i][g[i] + 1] == 10) {
        scanf("%d", &d[i][g[i] + 2]);
        g[i]++;
      }
      g[i] += 2;
    }
    for (i = 0; i < a; i++) {
      j = 0;
      c = 0;
      while (1) {
        if (d[i][j] == 10) {
          z[i][c] = 2;
          x[i][c] = 10;
          ss++;
          j++;
          aa[i][c] = j;
          c++;
        } else if (d[i][j] + d[i][j + 1] == 10) {
          z[i][c] = 1;
          x[i][c] = 10;
          j = j + 2;
          aa[i][c] = j;
          c++;
        } else if (d[i][j] + d[i][j + 1] != 10) {
          z[i][c] = 0;
          x[i][c] = d[i][j] + d[i][j + 1];
          j = j + 2;
          aa[i][c] = j;
          c++;
        } else if (j = g[i] - 1 && d[i][j] != 10) {
          x[i][c] = d[i][j];
          z[i][c] = 0;
          j++;
          aa[i][c] = j;
          c++;
        }
        if (j >= g[i])
          break;
      }
    }
    for (i = 0; i < a; i++) {
      for (j = 0; j < 9; j++) {
        q[i] += x[i][j];
        if (z[i][j] == 2) {
          if (z[i][j + 1] == 2) {
            if (z[i][j + 2] == 2)
              q[i] = q[i] + x[i][j + 1] + x[i][j + 2];
            else if (z[i][j + 2] != 2)
              q[i] = q[i] + x[i][j + 1] + d[i][aa[i][j + 1]];
          } else if (z[i][j + 1] != 2) {
            q[i] += x[i][j + 1];
          }
        } else if (z[i][j] == 1) {
          q[i] += d[i][aa[i][j]];
        }
      }
      q[i] = q[i] + x[i][9] + x[i][10] + x[i][11];
    }
    for (i = 0; i < a; i++) {
      for (j = i + 1; j < a; j++) {
        if (q[i] < q[j]) {
          ss = q[i];
          q[i] = q[j];
          q[j] = ss;
          ss = s[i];
          s[i] = s[j];
          s[j] = ss;
        } else if (q[i] == q[j]) {
          if (s[i] > s[j]) {
            ss = q[i];
            q[i] = q[j];
            q[j] = ss;
            ss = s[i];
            s[i] = s[j];
            s[j] = ss;
          }
        }
      }
    }
    for (i = 0; i < a; i++) {
      printf("%d %d\\n", s[i], q[i]);
    }
  }
  return 0;
}