Thread: print 2-dimensional array

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    122

    print 2-dimensional array

    Hello!
    I'm familarized with Matlab/scilab and other labs programing. So, I've got some dificults to manipulate Array in C.

    I need to use in C a instruction which compute, i.e.
    Code:
    matrix_display(2,:)
    But I read in C we need to access all rows and columns.

    So I write this code to "see" a print of array, but output is garbage

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define row_size 10
    #define column_size 7
    int matrix_display [row_size] [column_size]={ 
                                {1,1,1,1,1,1,0},//0
                                 {0,1,1,0,0,0,0},//1
                                 {1,1,0,1,1,0,1},//2
                                 {1,1,1,1,0,0,1},//3
                                 {0,1,1,0,0,1,1},//4
                                 {1,0,1,1,0,1,1},//5
                                 {0,0,1,1,1,1,1},//6
                                 {1,1,1,0,0,0,0},//7
                                 {1,1,1,1,1,1,1},//8
                                 {1,1,1,0,0,1,1},//9
                                };
    
    int main()
    {
    
    int i,j;
    
    for(i=0;i<row_size;i++)
    {
    
        for(j=0;j<column_size;j++)
    
        printf(" %d\n", matrix_display[i,j]);
    }
    
    }
    Where is my mistake?

    Is there a simple way to print all columns which correspond at 1 row?


    Thank you
    And sorry, this is a rookie question

  2. #2
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    Quote Originally Posted by marcoesteves View Post
    Hello!
    I'm familarized with Matlab/scilab and other labs programing. So, I've got some dificults to manipulate Array in C.

    I need to use in C a instruction which compute, i.e.
    Code:
    matrix_display(2,:)
    But I read in C we need to access all rows and columns.

    So I write this code to "see" a print of array, but output is garbage

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define row_size 10
    #define column_size 7
    int matrix_display [row_size] [column_size]={ 
                                {1,1,1,1,1,1,0},//0
                                 {0,1,1,0,0,0,0},//1
                                 {1,1,0,1,1,0,1},//2
                                 {1,1,1,1,0,0,1},//3
                                 {0,1,1,0,0,1,1},//4
                                 {1,0,1,1,0,1,1},//5
                                 {0,0,1,1,1,1,1},//6
                                 {1,1,1,0,0,0,0},//7
                                 {1,1,1,1,1,1,1},//8
                                 {1,1,1,0,0,1,1},//9
                                };
    
    int main()
    {
    
    int i,j;
    
    for(i=0;i<row_size;i++)
    {
    
        for(j=0;j<column_size;j++)
    
        printf(" %d\n", matrix_display[i,j]);
    }
    
    }
    Where is my mistake?

    Is there a simple way to print all columns which correspond at 1 row?


    Thank you
    And sorry, this is a rookie question
    it should be matrix_display[i][j]
    and put a new line character \n after one row is printed, looks better

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Thank you very much

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You should understand exactly what matrix[i][j] means b/c it will help you later when you realize exactly what is going on. The syntax here doesn't mean half as much as what is happening under the hood. So rather than spoon feeding you syntax we should probably attempt to help you understand 2D arrays and their relation to 1D arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of 2 dimensional arrays
    By toothpick in forum C Programming
    Replies: 1
    Last Post: 10-23-2010, 01:11 AM
  2. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  3. constantly print an array to the screen?
    By .exe in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2008, 10:41 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

Tags for this Thread