UMBC CS 201, Fall 06
 UMBC CMSC 201 Fall '06 CSEE | 201 | 201 F'06 | lectures | news | help Search Notes:

# Multi-dimensional Arrays as Parameters

## Example prototypes

### For a 2-dimensional array:

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

### For a 3-dimensional array:

```  void Print3DArray(int a[], int x, int y, int z);
```
or
```  void Print3DArray(int a, 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 optionally 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 * EMail: bogar@cs.umbc.edu * Modified by: Sue Evans * Mod. date: 2/21/04 * Mod reason: To meet current 201 standards * * This program was designed to show an example * of passing a two-dimensional array to a * function *************************************************/ #include <stdio.h> #define ROWS 7 #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() * * FillArray() fills each element in the 2-dimensional * array passed in with the product of each elements' * indices. * * Input: a two-dimensional array of ints, * the number of rows & the number of columns * Output: 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() * * FindMean() calculates the mean of the values stored in * the 2-dimensional array passed in. * * Input: a two-dimensional array of ints, the number * of rows & the number of columns * 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() * * PrintArray() prints the values stored in a 2-dimensional * array of ints in tabular form. * * Input: a two-dimensional array of ints, the number of * rows & the number of columns * Output: 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 % 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 0 5 10 15 20 0 6 12 18 24 The mean of all the values is 6.00 linux1 %