Thread: 2 dimentional array

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    15

    2 dimentional array

    Code:
    I am making a program to ask a user for input, and stored them in an array thereafter it supposed to print the information using a function called display. The problem I am having is when I try to compile it gives me a core dumped error. Help will be much appreciated. 
    
    
    here are the code
    
    #include<stdio.h>
    void display(int x[10][10]);
    int main(void)
    {
            int array[10][10];
            int n, m,j,i;
            printf("Enter number of rows: ");
            scanf("%d", &m);
            printf("Enter number of colums: ");
            scanf("%d", n);
            for (i=0; i<n; i++) {
                    for (j = 0; j<m; j++){
                            printf( "Enter rows[%d] and colums[%d] : ", i, j);
                            scanf( "%d", &array[i][j]);
                    }
            printf("array[%d][%d]", array[i][j]);
            }
            printf("array[%d] = [%d]", array[i][j]);
            display( array);
            return 0;
    }
    void display(int x[10][10]){
            int i, j;
            printf("Displaying:\n");
            for(i=0; i<10; i++)
                    for(j=0; j<10; j++)
                            printf("%d\n", x[i][j]);
    }

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Line #15; type of n is int rather than int *. You probably want to check scanf's return value as well, or just get a C reference (like K&R2) that doesn't teach you to write shotty code.

    edit: Oh, and there are some other issues too. Certain values of m and n can make your program access invalid elements of "array". The display function can also make your program blow up (accessing uninitialised values) if the values of m and n aren't both 10.
    Last edited by Barney McGrew; 04-16-2013 at 08:46 PM.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    15
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two Dimentional array problems
    By VASHtheCHIBI in forum C Programming
    Replies: 30
    Last Post: 01-16-2012, 11:31 PM
  2. Pointer to two dimentional array
    By lio in forum C Programming
    Replies: 2
    Last Post: 11-06-2010, 10:55 AM
  3. 3 dimentional array in C
    By jahanpanah in forum C Programming
    Replies: 3
    Last Post: 10-24-2010, 11:16 AM
  4. 2 dimentional array
    By theone1 in forum C++ Programming
    Replies: 10
    Last Post: 11-11-2007, 01:30 PM
  5. Two dimentional array
    By h3ro in forum C++ Programming
    Replies: 12
    Last Post: 04-17-2007, 01:01 AM