UMBC CS 201, Fall 02
UMBC CMSC 201 Fall '02 CSEE | 201 | 201 F'02 | lectures | news | help

Multi-dimensional Arrays as Parameters

Example prototypes

For a 2-dimensional array:

   
  void Print2DArray(int a[][5], int rows, int cols);
or
  void Print2DArray(int a[5][5], int rows, int cols);  

For a 3-dimensional array:

  void Print3DArray(int a[][4][4], int x, int y, int z);
or
  void Print3DArray(int a[4][4][4], int x, int y, int z);
Notice that all but the first square bracket must show the size of the dimension. The first square bracket may optioinally contain the size of first dimension as well.

What gets passed ?

Just like one-dimensional arrays, the address of the base of the array.

The Program

/************************************************* * Filename: passing2d.c * Author: Sue Bogar * Date Written: 2/25/98 * Section: 101 * SSN: 000-00-0001 * EMail: bogar@cs.umbc.edu * * This program was designed to show an example * of passing a two-dimensional array to a * function *************************************************/ #include <stdio.h> #define ROWS 5 #define COLS 5 /* Example prototype of a function that takes an ** array as an argument. NOTE: the empty square ** brackets for the first dimension. You can ** optionally put the size within the brackets. */ void FillArray (int array[][COLS], int rows, int cols); float FindMean (int array[ROWS][COLS], int rows, int cols); void PrintArray (int array[ROWS][COLS], int rows, int cols); int main ( ) { int array[ROWS][COLS]; float mean; FillArray (array, ROWS, COLS); mean = FindMean (array, ROWS, COLS); PrintArray (array, ROWS, COLS); printf ("\nThe mean of all the values is %.2f\n", mean); return 0; } /********************************************************* * Function: FillArray * Input: a two-dimensional array of ints * Output: the value of the product of each element's * indices to that element. * there is no return value *********************************************************/ void FillArray (int array[][COLS], int rows, int cols) { int i, j; /* Assign each element the value of the product of the two indices */ for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { array[i][j] = i * j; } } } /********************************************************* * Function: FindMean * Input: a two-dimensional array of ints * Output: returns the (float) mean of all the * elements in the array. ********************************************************/ float FindMean (int array[ROWS][COLS], int rows, int cols) { int i, j, num; float mean, total = 0.0; /* Find the sum of all the elements */ for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { total += array[i][j]; } } /* Calculate number of elements */ num = rows * cols; /* Find the mean */ mean = total/num; return mean; } /********************************************************* * Function: PrintArray * Input: a two-dimensional array of ints * Output: the array is printed in tabular form * there is no return value *********************************************************/ void PrintArray (int array[ROWS][COLS], int rows, int cols) { int i, j ; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { printf ("%4d", array[i][j]); } printf ("\n"); } }

The Sample Run

linux1[109] % a.out 0 0 0 0 0 0 1 2 3 4 0 2 4 6 8 0 3 6 9 12 0 4 8 12 16 The mean of all the values is 4.00 linux1[110] %
Last Modified - Friday, 23-Aug-2002 11:05:02 EDT


CSEE | 201 | 201 F'02 | lectures | news | help