Two-Dimensional Array Initialization (please help)

This is a discussion on Two-Dimensional Array Initialization (please help) within the C Programming forums, part of the General Programming Boards category; I am a beginnger programmer and I am trying to write a program that will initialize a 2-dimensional array with ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Windsor, Ontario, Canada
    Posts
    2

    Two-Dimensional Array Initialization (please help)

    I am a beginnger programmer and I am trying to write a program that will initialize a 2-dimensional array with zeros. Also, it will use a function to do the intializing. Unfortunately I can't get it to work.

    Here is the code I have so far:

    Code:
    
    #include <stdio.h>
    
    #define M 10
    #define N 5
    
    void InitializeArray2D ( int array[][N], int r, int c);
                        
     
    void main() 
    {
       int a [M][N];
    
       InitializeArray2D (a,M,N);
     
    }
    
    
    void InitializeArray2D (int array [][N], int r, int c) {
    
       int i,j;
    
       for (i=0; i<r; i++){
            for (j=0; j<c; j++){
                    int array [i][j] = 0;
                            }
                    }
       printf("%d", array[1][1]); /*test to see if initialization worked*/
    }

    Any help would be greatly appreciated!!
    Thanks...
    Phil

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    In your for loop, remove the "int" from in front of "array". You only need to put the type when declaring the variable.

    Also, main needs to return int, not void.

    Apart from that, it looks fine.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Location
    Windsor, Ontario, Canada
    Posts
    2
    got it working....thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Two Dimensional Array Input from Formatted Data
    By teedoff087 in forum C Programming
    Replies: 14
    Last Post: 04-29-2007, 01:46 AM
  3. Multi dimensional array
    By big146 in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 05:03 PM
  4. 2d arrays in C
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 04-20-2002, 11:09 AM
  5. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21