# Thread: Multidimension array problem. I can't figure out why the last column is different

1. ## Multidimension array problem. I can't figure out why the last column is different

I'm sorry. I really need help on this. I even made a shorter and simpler program that I think is enough to show my problem.

This is the example program:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
int i, j, deg, points;

printf("Enter degree of polynomial equation: "); scanf("%i", &deg);
printf("Enter number of points: "); scanf("%i", &points);
float xs[points-1], Fmat[points][deg];
for(i=0; i<points; i++)
{printf("x%i = ", i); scanf("%f", &xs[i]);}
printf("\n");

for(i=0; i<points; i++)
for(j=0; j<deg+1; j++)
Fmat[i][j]=pow(xs[i],1);

for(i=0; i<points; i++)
{for(j=0; j<=deg; j++)
printf("%ii%ij:%7.3f\t", i,j,Fmat[i][j]);
printf("\n");}

system("PAUSE");
return 0;
}```

Now, my problem is that it is printing (and probably storing) the right values EXCEPT on the last column.

This is greatly confusing me and I've spent my whole night trying to figure it out (to no avail, obviously). I'm sorry for the dumbness of my problem but will somebody please help me figure it out??

Sample output: Imageshack - output
Screenshot of program: Imageshack - programl

2. I'm surprised that compiles for you. Variable length arrays are not valid C++.

Code:
```for(i=0; i<points; i++)
for(j=0; j<deg+1; j++)
Fmat[i][j]=pow(xs[i],1);```
That code is overwriting the Fmat array. You are accessing Fmat[i][j] when j is equal to deg. This is not allowed since the array is not big enough to handle this. You are doing the same thing when you print out the array.

3. Originally Posted by bithub
I'm surprised that compiles for you. Variable length arrays are not valid C++.

Code:
```for(i=0; i<points; i++)
for(j=0; j<deg+1; j++)
Fmat[i][j]=pow(xs[i],1);```
That code is overwriting the Fmat array. You are accessing Fmat[i][j] when j is equal to deg. This is not allowed since the array is not big enough to handle this. You are doing the same thing when you print out the array.
I'm sorry. I only understood about the part where "variable length" arrays are not valid c++. But are they valid for c??? But anyway, I've encountered an error probably regarding that but I "attempted to fix it" by declaring the array after requesting an input for the "variable" size.

I've tried using multidimension array before and they worked. However, now that I'm trying to do a "matrix" sort-of problem (sorry I don't know technical terms), I'm having that difficulty with the last column.

However, I when I accidentally labeled j as i and vice versa, the output was okay. I was just not sure if the values were stored properly.

4. PS: I edited the program and replaced the variable sizes as constants and the output was still the same. The last column still ended up with different values. I don't know where they came from. I can't figure it out.

5. Originally Posted by warmsheet
But are they valid for c???
Yes, they were added by the C99 standard.

Another way of looking at your error is this:
Code:
```char some_array[5];
for(int i = 0; i < 5+1; i++)
some_array[i] = something;```
You are going past the end of the array! If an array is declared as having 5 indexes, that means you can access indexes 0, 1, 2, 3, and 4. You cannot access index 5 because you declared the array as having 5 total indexes. Accessing index 5 means you are accessing the 6th index. I know it's a little confusing, but once you realize that indexes start at zero, not one, it is easier to understand.

6. Another way of looking at your error is this:
Code:
```char some_array[5];
for(int i = 0; i < 5+1; i++)
some_array[i] = something;```
You are going past the end of the array! If an array is declared as having 5 indexes, that means you can access indexes 0, 1, 2, 3, and 4. You cannot access index 5 because you declared the array as having 5 total indexes. Accessing index 5 means you are accessing the 6th index. I know it's a little confusing, but once you realize that indexes start at zero, not one, it is easier to understand.[/QUOTE]

WOW THANKS A LOT! So that was it. ^^;

It was my mistake to actually think that declaring as having 5 indexes (as what you termed ^^ would give me 0,1,2,3,4,5!! (I did consider starting at 0, BUT I did not consider ending at size-1. ^^;

Thanks. I had to think for a few minutes to actually realize that. I also edited my program and so far it gave better result. =) WOW thanks a lot. And I've been having this problem since last night.