Home > Programming > Functions using the "C" programming language > example05.c

 

Previous

Next


/*
    In this example we demonstrate how to pass a three dimention array
    as paramenter in a function
*/

#include <stdio.h>

#define XSIZE 2
#define YSIZE 4
#define ZSIZE 3

/* Function prototype */
double sum3D(float [][YSIZE][ZSIZE],int);

void main(void)
{
    float nums[XSIZE][YSIZE][ZSIZE] = /* Initial values */
        {{{0.3, 0.4, 0.5},
          {0.6, 0.7, 0.8},
          {0.9, 1.0, 1.1},
          {1.2, 1.3, 1.4}},
         {{1.3, 1.4, 1.5},
          {1.6, 1.7, 1.8},
          {1.9, 2.0, 2.1},
          {2.2, 2.3, 2.4} }};

    printf("%lf\n",sum3D(nums,XSIZE));
}

/* Calculate the sum of the elements in a three dimention array */
double sum3D(float n[][YSIZE][ZSIZE],int x)
{
    int i,j,k;
    double sum=0.0;
    for(i=0;i<x;i++) for(j=0;j<YSIZE;j++) for(k=0;k<ZSIZE;k++) sum+=n[i][j][k];
    return sum;
}


© 2004 Jim Valavanis

Previous

Next

1