2

I am a novice programmer, so please deal with me. I'm writing a program to analyze data. When I execute the program, I receive a "floating point exception" and I have no clue why. Here is the section of code where the error is originating from. From what I can tell, the if statement seems to be the problem but I do not understand why. Any help you can give is greatly appreciated!

double tArray[600][49];

void main() {
  double finalArray[600][0]
  double n = 0;
  int h = 0;
  try {
    for (int i = 0; i < 600; j++) {
      for (int j = 1; j < 16; j++) {
        h++;
        n = tArray[i][j * 3 - 1] - tArray[i][j * 3 - 2];
        double t = -30;
        if (n < t) {
          finalArray[i][0] = tArray[h][3 * j] - tArray[h + t][3 * j];
          h++;
        }
      }
    }
  }
}
Adam Liss
  • 47,594
  • 12
  • 108
  • 150
Tyler
  • 27
  • 1
  • 5
  • `finalArray[i][0]` does go out of bounds. – chris Oct 14 '12 at 01:13
  • You're incrementing 'h' inside the inner loop, and then using it as the first index in tArray - it will go over 600 pretty darn quickly. – Paul Tomblin Oct 14 '12 at 01:19
  • We can assume that missing semicolon in `double finalArray[600][0]` is just a typo, right? – cHao Oct 14 '12 at 01:23
  • Is the `j++` in the `for(int i = 0; i < 600; j++)` loop a typo? What is the `try` block for? This isn't your real code is it? It's a mess. – Blastfurnace Oct 14 '12 at 01:24
  • Throw away whatever book told you to use `void main()`. It's `int main()`. – Keith Thompson Oct 14 '12 at 01:50
  • You're not getting a floating-point exception with the code you posted, because it has numerous syntax errors. Please post your real code. Copy-and-paste it, don't re-type it. @cHao: Type missing semicolon is probably a typo; the `[0]` could also be a typo. – Keith Thompson Oct 14 '12 at 01:53

2 Answers2

1

Try declaring finalArray as:

double finalArray[600][1];

It appears that the original declaration did not allocate any space for the elements.

If not try allocating only a single dimensional array like this:

double finalArray[600];

Hope this helps!

Anand V
  • 124
  • 6
1

Declaring double finalArray[600][0] means the array is 600 elements by 0 elements. I think you want the second dimension to be 1. Remember, you need to declare the number of rows and columns, but indices start at 0.

Adam Liss
  • 47,594
  • 12
  • 108
  • 150