L-39 MCS 260 Mon 19 Nov 2001

Below are the full versions of the programs we discussed in class.
/* L-39 MCS 260 Mon 19 Nov 2001 : three dimensional matrices

The program below illustrates the use of three dimensional matrices.
Assume a temperature distribution of a room is described by the
function T(x,y,z) = 100*exp(-(x+y+z)), which gives the temperature
at point with coordinates (x,y,z).  For a heat source at the origin,
we see the temperatures decaying as we move away from the origin. 

This program should be compiled with the option "-lm" as we use
the exp() function of the math libary.                             */

#include<math.h>
#include<stdio.h>

/* dimensions of a grid */

#define  NX  3
#define  NY  5
#define  NZ  2

void print ( int n1, int n2, int n3, double a[][n2][n3] );
/* prints the content of the n1 x n2 x n3-matrix a on screen */

double average ( int n1, int n2, int n3, double a[][n2][n3] );
/* returns the average of the n1 x n2 x n3-matrix */

int main ( void )
{
   double temp[NX][NY][NZ];
   int i,j,k;

   for (i=0; i<NX; i++)
      for (j=0; j<NY; j++)
         for (k=0; k<NZ; k++)
            temp[i][j][k] = 100*exp((double) -i-j-k);

   printf("The temperature distribution :\n");
   print(NX,NY,NZ,temp);

   printf("The average temperature : %.15lf\n", average(NX,NY,NZ,temp));

   return 0;
}

void print ( int n1, int n2, int n3, double a[][n2][n3] )
{
   int i,j,k;

   for (i=0; i<n1; i++)
      for (j=0; j<n2; j++)
         for (k=0; k<n3; k++)
            printf("  a[%d][%d][%d] = %.15lf\n",
                   i, j, k, a[i][j][k]);
}

double average ( int n1, int n2, int n3, double a[][n2][n3] )
{
   double sum = 0.0;
   int i,j,k;

   for (i=0; i<n1; i++)
      for (j=0; j<n2; j++)
         for (k=0; k<n3; k++)
            sum += a[i][j][k];

   return sum/(n1*n2*n3);
}
Unfortunately, time was too short to cover the materials for this program in detail. We will come back to this in the next lecture :
/* L-39 MCS 260 Mon 19 Nov 2001 : three dimensional matrices

The program below illustrates the use of three dimensional matrices.
Assume a temperature distribution of a room is described by the
function T(x,y,z) = 100*exp(-(x+y+z)), which gives the temperature
at point with coordinates (x,y,z).  For a heat source at the origin,
we see the temperatures decaying as we move away from the origin. 

Here we use dynamic memory allocation to let the user give in
the size of the room.

This program should be compiled with the option "-lm" as we use
the exp() function of the math libary.                             */

#include<math.h>
#include<stdio.h>

void print ( int n1, int n2, int n3, double *a );
/* prints the content of the n1 x n2 x n3-matrix a on screen
   this function also illustrates the pointer arithmetic     */

double average ( int size, double *a );
/* returns the average of the n1 x n2 x n3-matrix, size = n1xn2xn3 */

int main ( void )
{
   double *temp,*p;
   int nx,ny,nz,i,j,k;

   printf("Give number of points along x-axis : ");
   scanf("%d", &nx);
   printf("Give number of points along y-axis : ");
   scanf("%d", &ny);
   printf("Give number of points along z-axis : ");
   scanf("%d", &nz);

   temp = (double*) calloc(nx*ny*nz,sizeof(double));   /* allocation */

   p = temp;
   for (i=0; i<nx; i++)
      for (j=0; j<ny; j++)
         for (k=0; k<nz; k++)
            *(p++) = 100*exp((double) -i-j-k);

   printf("The temperature distribution :\n");
   print(nx,ny,nz,temp);

   printf("The average temperature : %.15lf\n", average(nx*ny*nz,temp));

   free(temp);         /* deallocation of memory */

   return 0;
}

void print ( int n1, int n2, int n3, double *a )
{
   int i,j,k;
   double *p,*q;

   p = a;
   for (i=0; i<n1; i++)
      for (j=0; j<n2; j++)
         for (k=0; k<n3; k++)
         {
            q = a + (i*n2 + j)*n3 + k;
            printf("  a[%d][%d][%d] = %.15lf = %.15lf\n",
                   i, j, k, *(p++), *q );
         }
}

double average ( int size, double *a )
{
   double sum = 0.0;
   double *p;

   for (p = a; p < a+size; p++) sum += *p;

   return sum/size;
}