Initializing an Array to Zero

This is a discussion on Initializing an Array to Zero within the C Programming forums, part of the General Programming Boards category; I am trying to initialize a two-dimensional array to 0. Here is my code for the function. I keep getting ...

  1. #1
    Unregistered
    Guest

    Initializing an Array to Zero

    I am trying to initialize a two-dimensional array to 0. Here is my code for the function.

    I keep getting an error telling me I am re-initializing the array. I need to set the array magic[25][25] = 0.
    Here is my code. The help would be greatly appreciated.

    void magicSquare( int size, int magic[25][25])
    {
    /*local definitions*/

    int row = 0;
    int col = size / 2;
    int c;
    int magic[25][25] = {0};

    magic[row][col] = 1;

    for(c=2; c <= size * size; c++)
    {
    row --;
    col++;

    if(aboveTop(row,col,size))
    row = size - 1;
    if(outRight(row,col,size))
    col = 0;
    if(filledSquare(row, col, size))
    col = size - 1;
    }magic[row][col] = c;
    return;
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Take a look at the parameters you've passed and the parameters which you declare within the function. Especially "magic".

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    19

    help maybe

    So this doesn't look like your code too much but I think it might work just as well.



    int magic[25][25];
    memset( magic, 0, sizeof(magic));


    or

    int i = 0;
    int magic[25][25];
    for(i=0;i<sizeof(magic);i++){
    (magic + i) = 0;
    }

    I dunno, but they might work for what you're doing...

    good luck


    ~Gm
    ~good monkeys, Excelent typewritters!...

  4. #4
    Unregistered
    Guest

    Wink

    Question:

    Is the function to generate the magic square of odd sizes, if so I have some code to help.

    For this
    void magicSquare( int size, int magic[25][25])

    I would use

    void magicSquare (int size, int magic[][25])


    Also, a size 25 magic square may not fit on the screen completely. I think I can get up to a size 15.

    jc

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void magicSquare( int size, int magic[25][25]) 
    { 
        /* local definitions*/ 
    
        int row = 0; 
        int col = size / 2; 
        int c; 
        int magic[25][25] = {0};
    I'm surprised your code compiles. You can't have a local variable
    who's name is the same as a parameter's name.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    9

    answer



    as you are using new style for function declaration you cant again redeclare the same array name again,it will give comile time error.

  7. #7
    Sayeh
    Guest
    If all you want is to initialize the array to zero, why not try this:


    Code:
    ...
    for(i=0;i<25;i++)
       for(j=0;j<25;j++)
          magic[i][j] = 0;
    ...
    seems relatively straightforward. There are other faster, more advanced techniques, but this will do what you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Initializing a 2D Array in C
    By Cell in forum C Programming
    Replies: 20
    Last Post: 03-21-2009, 01:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Initializing char * array
    By Tia in forum C Programming
    Replies: 6
    Last Post: 03-11-2003, 05:19 PM

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