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

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    4

    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??

    Thanks in advance.


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

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    4
    Quote Originally Posted by bithub View Post
    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. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    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. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote 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.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    4
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question: sizeof() problem in function
    By Xeyide in forum C Programming
    Replies: 3
    Last Post: 09-04-2009, 12:05 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM