Pass a 2D Array to a Function (No Pointers!)

Suppose you have an array of integers which represents a magic square: All the rows and columns — even the two diagonals — add to the same total. To prove it, you create a function, confirm_magic() that processes the array and validates the math. You have just one problem . . .

If you want to pass a two-dimensional array (a grid) to a function, you must use pointers. Specifically, you summon the dratted ** thing, which everyone avoids.

To keep your sanity, you instead decide to use a single-dimension array, which is how all arrays are stored internally anyhow. (See last week’s Lesson.)

Use this format to pass an array to a function:

confirm_magic(array)

where “array” otherwise appears as array[] in the code. And to declare an array as a function’s argument, you use this prototype:

int confirm_magic(array[]);

The array marches off to the function, is processed as a 2D array, and then the results are returned. Magic. Or not.

Click here to view the entire code, which is lengthy, but contains a lot of similar pieces.

The grid[] array is single-dimension:

    int grid[25] = {
        25, 13,  1, 19,  7,
        16,  9, 22, 15,  3,
        12,  5, 18,  6, 24,
         8, 21, 14,  2, 20,
         4, 17, 10, 23, 11
    };

I formatted it to look like a 2D array, though it’s not declared as such.

In the main() function, the code uses sleight-of-hand to display grid[] in rows and columns. Then the array is passed to the confirm_magic() function, where its rows, columns, and diagonal offsets are calculated and tallied. The value 1 or 0 is returned for TRUE or FALSE, indicating whether the grid is a magic square.

Here is the rows calculation from the confirm_magic() function. The array is named square[] within the function:

    /* calculate rows */
    for(r=0;r<5;r++)
    {   
        sum=0;
        for(c=0;c<5;c++)
            sum+=square[r*5+c];
        if(sum != 65)
            return(0);
    }

Variables r and c represent rows and columns (actually offsets) in the array. The specific code square[r*5+c] represents array elements from row r. So the first row, 0, is processed with columns (c) 0 through 4. Then comes row 1, and onward. Variable sum accumulates the values and is compared with 65, the desired total for this particular magic square. If the value isn’t 65, the function returns 0, FALSE.

For the column calculation, the following statement is used:

sum+=square[c+r*5];

Variable c loops from 0 to 4, representing “columns” within the single-dimension array. Then the row value, r, loops from 0 to 4. The function’s math tallies the total of each “column” in the array, accumulating the total in variable sum.

Similar loops process the diagonals, using math to determine which array elements fall on the diagnoal.

Here is a sample run:

Magic Square
  25  13   1  19   7
  16   9  22  15   3
  12   5  18   6  24
   8  21  14   2  20
   4  17  10  23  11
Yes, it's a magic square

If you alter the array, the confirm_magic() function returns FALSE and an appropriate message is output.

Leave a Reply