Thread: Multidimensional arrays.

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    Multidimensional arrays.

    Hi guys,
    I'm a noob so please bear with me.
    I'm trying to write some values in a multidimensional array with a variable length and then to print the values in row/col format. The problem is that it prints out wrong values. In the case of a 2*2 matrix it prints the last 2 values entered. Why is that?
    Don't mind the commented out lines.
    Thank you, guys.
    Code:
    Code:
    #include <stdio.h>
    //#define MAX 100
    
    int main(void)
    {
        int rowA, colA, rowB, colB;
        rowA = colA = rowB = colB = 0;
        int i, j;
    //    int N = -1;
        int A[rowA][colA];//, B[0][0], C[0][0];
    
    //    while ((N <= 0) || (N > MAX))
    //    {
    //        printf("Enter the No. of elements for arrays >>> ");
    //        scanf("%i", &N);
    //        if ((N > MAX) || (N <= 0))
    //        {
    //            printf("Up to '100' and bigger than '0'.\n");
    //            //printf("Enter the No. of elements for arrays >>> ");
    //        }
    //    }
    
        printf("\nEnter the No. of rows for matrix 'A' >>> ");
        scanf("%i", &rowA);
        printf("\nEnter the No. of cols for matrix 'A' >>> ");
        scanf("%i", &colA);
    //    printf("\nEnter the No. of cols for matrix 'B' >>> ");
    //    scanf("%i", &colB);
    
    //    rowB = colA;
    
        for (i = 0; i < rowA; i++)
        {
            for (j = 0; j < colA; j++)
            {
                printf("\nEnter the value of A[%i][%i] >>> ", i, j);
                scanf("%i", &A[i][j]);
                printf("%i", A[i][j]); // checking if values has been entered corectly
            }
        }
    
    //    for (i = 0; i < rowB; i++)
    //    {
    //        for (j = 0; j < colB; j++)
    //        {
    //            printf("\nThe value of B[%i][%i] is >>> ", i, j);
    //            scanf("%i", &B[i][j]);
    //        }
    //    }
    
        printf("\nThis is the matrix 'A':\n");
        for (i = 0; i < rowA; i++)
        {
            for (j = 0; j < colA; j++)
            {
                printf("%i ", A[i][j]);
            }
            printf("\n");
        }
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You are not thinking that array resize themselves, right?

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Quote Originally Posted by Bayint Naung View Post
    You are not thinking that array resize themselves, right?
    Sorry, what do you mean?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your sizing of the array, is a real problem.

    You have to size your arrays by a value that you have set, beforehand. Changing any of the row or column sizes later, will have no effect on the array that was declared with the old value/s.

    If you really need to set the row and col variables for the array, then you need to skip declaring the array at first, and just do something like this:

    Code:
    int **myArray;
    Then, after you get the right row and column sizes, you can malloc the memory for the array. It's not a really trivial bit of code.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Basically, you are declaring an array A[0][0] since rowA and colA are zero when that line gets executed. Then afterward, you are changing the value of rowA and colA. Note that this WILL NOT change the size of the array you declared earlier. The variables rowA and colA are not associated in any way with the array after that declaration line is done.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    After Adak's comment

    I managed to pull it off, but I still didn't fully understood what happens at memory level with this so called " array resizing". So it will be great if anyone wishes to direct me to any kind of reference. Thank you, guys.
    Here is the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int rows, cols, i, j;
        int **matrix_A;
    
        printf("\nEnter the No. of rows for matrix 'A' >>> ");
        scanf("%i", &rows);
        printf("\nEnter the No. of cols for matrix 'A' >>> ");
        scanf("%i", &cols);
    
        matrix_A = malloc(rows * sizeof(int *));
        for (i = 0; i < rows; i++)
        {
            matrix_A[i] = malloc(cols * sizeof(int));
        }
    
        for (i = 0; i < rows; i++)
        {
            for (j = 0; j < cols; j++)
            {
                printf("\nEnter the value of A[%i][%i] >>> ", i, j);
                scanf("%i", &matrix_A[i][j]);
            }
        }
    
        printf("\nThis is the matrix 'A':\n\n");
        for (i = 0; i < rows; i++)
        {
            for (j = 0; j < cols; j++)
            {
                printf("%i ", matrix_A[i][j]);
            }
            printf("\n");
        }
    
        return 0;
    }
    Last edited by gabrix; 09-11-2010 at 09:39 AM.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Here is the second solution after KBriggs's comment:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int rowA, colA;
        rowA = colA = 0;
        int i, j;
    
        printf("\nEnter the No. of rows for matrix 'A' >>> ");
        scanf("%i", &rowA);
        printf("\nEnter the No. of cols for matrix 'A' >>> ");
        scanf("%i", &colA);
    
        int A[rowA][colA];
    
        for (i = 0; i < rowA; i++)
        {
            for (j = 0; j < colA; j++)
            {
                printf("\nEnter the value of A[%i][%i] >>> ", i, j);
                scanf("%i", &A[i][j]);
                printf("%i", A[i][j]); // checking if values has been entered corectly
            }
        }
    
        printf("\nThis is the matrix 'A':\n\n");
        for (i = 0; i < rowA; i++)
        {
            for (j = 0; j < colA; j++)
            {
                printf("%i ", A[i][j]);
            }
            printf("\n");
        }
    
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That looks a LOT better - well done (although I didn't run the code).

    Note that there is NO resizing done here - you made a pointer to a pointer - and then gave it a good address with malloc().

    Then you repeated that in a loop, but this time, assigning each row pointer, to a valid address.

    Resizing arrays would use realloc(), and it has it's own little pattern to be followed. It is best to malloc() more than you expect to use, and then resize the array, in blocks of enough size to prevent having to resize the array, over and over.

    One resize of 60 rows, is better than 20 resizes, of 3 rows each.
    Last edited by Adak; 09-11-2010 at 09:48 AM.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    Smile

    Quote Originally Posted by Adak View Post
    That looks a LOT better - well done (although I didn't run the code).
    Thank you, Adak. I was going out of my mind with this and, I'm still struggling to understand the process at the lowest level. But now I can sleep tonight.
    Last edited by gabrix; 09-11-2010 at 10:02 AM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Think of an array like an icecube tray... each cell can hold one value. Once it's made, that's how big it is.

    You can't change the size of static array because that would displace existing data. Memory is allocated contiguously as row0......row1.......row2..... etc. If you change the size of row 1, you will overwrite the data in row2 and now your array is filled with garbage.

    Once you make an array of any size, that's how big it is.

    Even using malloc, the array dimensions are determined by the amount of memory you reserved. and trying to plug in an extra row or remove a column will have exactly the same result as with a static declaration. Realloc overcomes this, up to a point, but it's not perfect either.
    Last edited by CommonTater; 09-11-2010 at 10:35 AM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The first solution is bad because it doesn't free what it allocates.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Quote Originally Posted by Elysia View Post
    The first solution is bad because it doesn't free what it allocates.
    Should I use free(matrix_A) after it prints out the matrix values?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    One free for every call to malloc, in reverse order.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays in a function prototype
    By Enanito01478 in forum C Programming
    Replies: 2
    Last Post: 10-11-2009, 01:10 AM
  2. Multidimensional arrays
    By Niels_M in forum C Programming
    Replies: 51
    Last Post: 09-12-2009, 03:16 PM
  3. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  4. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 10:35 PM
  5. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM