Thread: Printing 2D Array

  1. #1
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72

    Printing 2D Array

    I am new to 2D arrays.

    I am trying to print out the array.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        int array[3][4];
    
        int i, k;
    
        for( i = 0; i < 3; i++ )
            for( k = 0; k < 4; k++ )
            {
            printf("The first array is: %d\n", array[i] );
            printf("The second array is: %d\n", array[k] );
            }
    }
    Should print out:

    {0, 1, 2, 3,
    4, 5, 6, 7,
    8, 9, 10, 11}

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by BB89 View Post
    I am new to 2D arrays.

    I am trying to print out the array.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        int array[3][4];
    
        int i, k;
    
        for( i = 0; i < 3; i++ )
            for( k = 0; k < 4; k++ )
            {
            printf("The first array is: %d\n", array[i] );
            printf("The second array is: %d\n", array[k] );
            }
    }
    Should print out:

    {0, 1, 2, 3,
    4, 5, 6, 7,
    8, 9, 10, 11}
    You want to print i and k, not array[i] and array[k].

    It still won't print what you write that it "should", because on the very first loop, it will print 0, 0, since both i and k will be zero.

    You are trying to print out a 2D array, as if it were a 1D array, and there is a formula for that.

    Try and work it out, by hand. (hint: don't print either i or k, Think of i and k working together,)

  3. #3
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Do I need the second for loop?

    can I do just one loop

    if i changed the array to array[4][4]

    Code:
    for( i = 0; i < 4; i++ )

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, you need the second for loop.

    What would happen if i and k got "married". What was two, becomes one value.

  5. #5
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Ok, one last question. What do you mean when you said

    Code:
    You are trying to print out a 2D array, as if it were a 1D array.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    2D = Two dimension array (one with rows and columns). Like yours. Array[][].

    1D = One dimension array (just a sing row in the array). Array[]

    You WANT to print out your 2D array, as a 1D array. That was me going "OK, now I know what BB89 wants".

  7. #7
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Ok, understood. Im gonna work on it.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    Quote Originally Posted by BB89 View Post
    I am new to 2D arrays.

    I am trying to print out the array.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        int array[3][4];
    
        int i, k;
    
        for( i = 0; i < 3; i++ )
            for( k = 0; k < 4; k++ )
            {
            printf("The first array is: %d\n", array[i] );
            printf("The second array is: %d\n", array[k] );
            }
    }
    Should print out:

    {0, 1, 2, 3,
    4, 5, 6, 7,
    8, 9, 10, 11}
    im confused. how could you possibly get that output if you declare but dont initialise

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oddly, he doesn't need to print the values of the array[], at all. All he needs to do, to get *THAT* output, is to print the right combination of i and k.

    Once he does that, he'll know the equation to print up (or change up), any 2D array, into (or as), a 1D array. My Sudoku program does this, that's why I want him to figure this out for himself. Six months from now, there's a good chance he will still remember it. Otherwise, probably not.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    Quote Originally Posted by Adak View Post
    Oddly, he doesn't need to print the values of the array[], at all. All he needs to do, to get *THAT* output, is to print the right combination of i and k.

    Once he does that, he'll know the equation to print up (or change up), any 2D array, into (or as), a 1D array. My Sudoku program does this, that's why I want him to figure this out for himself. Six months from now, there's a good chance he will still remember it. Otherwise, probably not.
    oh ok.

    is this a common programming practice though?

    i ask cause couple weeks ago I had to code up a BFS type algorithm on a grid represented as a string, and not a 2d array. it was painful since I had to account for a bunch of edge cases which would have been much easier to handle if it were a simple 2d array

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Brain_Child View Post
    oh ok.

    is this a common programming practice though?

    i ask cause couple weeks ago I had to code up a BFS type algorithm on a grid represented as a string, and not a 2d array. it was painful since I had to account for a bunch of edge cases which would have been much easier to handle if it were a simple 2d array
    If you like solving problems with arrays, then it's a great trick to know. If you always use linked lists, etc., as your favorite data structure, then no.

    Getting that sense of using array indexes for tricks like this, is invaluable, imo.

    If the clue about i and k being "married" wasn't enough, I don't know what more of a clue could be given, short of just spelling it out.

    I should preface all this by saying I'm a hobby coder, not a pro.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    well if you need another clue, I wrote this program to help me determine the relationship

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        int array[3][4];
    
        int i, k, j=0;
    
        for( i = 0; i < 3; i++ )
            for( k = 0; k < 4; k++ )
            {
            printf("%d %d %d\n",i, k, j++);
    
            }
    }
    running that code should allow you to see the pattern a bit easier

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array of threads?
    By shorinhio in forum C Programming
    Replies: 2
    Last Post: 01-04-2007, 04:02 PM
  2. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 09:19 AM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM