Thread: Cannot understand this multi dimensional array concept

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    28

    Cannot understand this multi dimensional array concept

    Hello everyone,

    Am learning C programming from "Programming In C" by "Stephen Kochan". The following para has been copied from the book straightaway coz I want you guys to explain me the part in bold.


    As with one-dimensional arrays, it is not required that the entire array be initialized. A statement such as
    int M[4][5] = {
    { 10, 5, -3 },
    { 9, 0, 0 },
    { 32, 20, 1 },
    { 0, 0, 8 }
    };
    only initializes the first three elements of each row of the matrix to the indicated values. The remaining values are set to 0 . Note that, in this case, the inner pairs of braces are required to force the correct initialization.Without them, the first two rows and the first two elements of the 3rd row would have been initialized instead. (Verify to yourself that this is the case.)


    Please help
    Last edited by exus69; 11-13-2011 at 10:50 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One of the great things about programming is the ease with which you can test these things out, for yourself. Try it and print it up. See what you get. Don't be afraid to go explore these things.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    The result that am getting on executing the code(with or without the inner braces) is 2293440!! Now it has confused me even more. I tried it the following way:
    Code:
    #include <stdio.h>
    
    int main ()
    
    {
    
        int M[4][5] = { 10, 5, -3, 9, 0, 0, 32, 20, 1, 0, 0, 8 } ;
    
    
        printf ("%i", M) ;
    
        return 0 ;
    
    }
    AND

    Code:
    #include <stdio.h>
    
    int main ()
    
    {
        
        int M[4][5] = {
                        { 10, 5, -3 },
                        { 9,  0,  0 },
                        { 32, 20, 1 },
                        {  0,  0, 8 }
                      };
                      
        printf ("%i", M) ;
        
        return 0 ;
        
    }
    Please help

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,803
    > printf ("%i", M) ;
    This does NOT print the whole array!
    It's just (badly) taking the pointer to the start of the array - &M[0][0], and printing it in some integer representation.

    Try for loops to print each M[row][col] instead.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    Thanks Salem. Now am getting the result 10000100021473157120 after making
    the following changes to the code. I dont think this is right.

    Code:
    #include <stdio.h>
    
    int main ()
    
    {
    
        int M[4][5] = {
                        { 10, 5, -3 },
                        { 9,  0,  0 },
                        { 32, 20, 1 },
                        {  0,  0, 8 }
                      };
    
        int i, k ;
    
        for (i = 0, k = 0; i <= 3, k <= 4; i++, k++)
        printf ("%i%i", M[i][k]) ;
    
    
        return 0 ;
    
    }
    Please help
    Last edited by exus69; 11-13-2011 at 11:28 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The easiest and clearest way to work with a 2D array print out, is to use an outer for loop to handle the row logic, and an inner for loop to handle the column logic, within each row.

    Use variable names that are immediately meaningful to you, such as row and col, or r and c, instead of i and k, etc.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    Quote Originally Posted by Adak View Post
    The easiest and clearest way to work with a 2D array print out, is to use an outer for loop to handle the row logic, and an inner for loop to handle the column logic, within each row.
    Hi Adak,

    It'll be great if you or anyone can give me an example of the same with respect to the above program.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    23
    Quote Originally Posted by exus69 View Post
    Hi Adak,

    It'll be great if you or anyone can give me an example of the same with respect to the above program.
    You may find these tutorial useful:

    Basic matrix know-how
    Dystopian Code: How to Create, Assign Values to and Output a Matrix in C

    Other matrix tutorials (sum, product, etc) that operate with more advanced concepts:
    Dystopian Code: C

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    Ok lets consider a very simple code of a multi dimensional array.

    Code:
    #include <stdio.h>
    
    int main ()
    
    {
        int sun[2][3] = {
                            {0, 1, 2},
                            {3, 4, 5}
    
                        } ;
    
        int r, c ;
    
        for (r = 0, c = 0; r <= 1, c <= 2; r++, c++)
        printf ("%i", sun[r][c]) ;
    
        return 0 ;
    
    }
    Instead of the output 012345 am getting a weird number 044200192.
    What is the mistake here? The program is supposed to display the digit in this order right? [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] which means it should display 012345 right?

    Please help

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Let's consider that you need to consider the replies you have been given on this matter:

    Use nested for loops (that's two for loops one for each dimension of a 2D array), and work from that. You didn't look through the tutorial given in the link, did you?

    I remain quite unclear on this whole concept of asking for help, getting exactly the help you asked for, and then returning with unchanged types of code.

    Honestly, if you hold your breath long enough, you will be complaining next about the lack of air, I just know it.
    Last edited by Adak; 11-14-2011 at 06:53 AM.

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    Quote Originally Posted by Adak
    The easiest and clearest way to work with a 2D array print out, is to use an outer for loop to handle the row logic, and an inner for loop to handle the column logic, within each row.
    Quote Originally Posted by exus69 View Post
    Hi Adak,

    It'll be great if you or anyone can give me an example of the same with respect to the above program.
    Hello Adak,

    I did ask you(or anyone) who could show me practical code what you actually mean.
    Am a real noob only dependant on this forum to get help on C but instead of showing
    the code ur criticizing me of not following what you said.

    If you notice my code in post#9, I did follow what you suggested in post#6 of using
    meaningful variable names like r for rows and c for columns. Am sorry if I offended you
    in any way...

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The link in post #8 of your thread has ALL the info you were asking for, including examples. How to get values to print from the 2D array, how to enter values into a 2D array - it's ALL right there, with code examples.

    Using more meaningful names for 2D array indices is helpful, but so is studying the tutorial links that answer your questions, in greater detail than we can, in a post or two.

    Go! Study! Learn! USE the resources that you have been given.

    Oh don't think for one moment that I won't criticize a student who won't use the study aids that they have been given! Wasting your brain is a sin in my book, and I'll nail up your hide on my barn door -- friend!

  13. #13
    Charming Registered User
    Join Date
    May 2011
    Posts
    49
    exus69, Try to write with words how would you print array's elements in the order you want. Something like....

    Code:
    Go to the first row (i = 0) , check if that row exists (i < max rows).
       Go to the first element of that row, (k = 0), check if the element exist (k < max cols)
          Print that element.
       Move to the next element in that row. (++k)
    Move to the next row. (++i)
    Always read any tutorial recommended in here.
    If you are the smartest person in the room, you are in the wrong room.

  14. #14
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    Hello Adak,

    I agree its my mistake. I should have gone through that link thoroughly instead of asking here again. But I wanna know whats the technical difference in my approach
    Code:
     for (r = 0, c = 0; r <= 1, c <= 2; r++, c++)
     printf ("%i", sun[r][c]) ;
    and that link approach
    Code:
     for (r = 0; r <= 1; r++)
    {
       for (c = 0; c <= 2; c++)
       printf ("%i", sun[r][c]) ;
    
    }
    Please help

  15. #15
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    Quote Originally Posted by ardavirus View Post
    Code:
    Go to the first row (i = 0) , check if that row exists (i < max rows).
       Go to the first element of that row, (k = 0), check if the element exist (k < max cols)
          Print that element.
       Move to the next element in that row. (++k)
    Move to the next row. (++i)
    Hello ardavirus,

    You answered my question even before I was writing my question!!!
    But dont you think, technically, the following code does the same thing what u've mentioned:

    Code:
    for (r = 0, c = 0; r <= 1, c <= 2; r++, c++)
      printf ("%i", sun[r][c]) ;
    Please help
    Last edited by exus69; 11-14-2011 at 08:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  2. multi-dimensional array
    By shuo in forum C++ Programming
    Replies: 4
    Last Post: 06-16-2008, 01:03 AM
  3. Multi dimensional array
    By big146 in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 05:03 PM
  4. multi-dimensional array
    By mcorn in forum C Programming
    Replies: 2
    Last Post: 08-04-2002, 09:14 AM
  5. allocate mem for a multi-dimensional array?
    By gunne in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2002, 05:50 AM