/* heat.c */ #include "heatSupport.h" void main() { double p[V][H]; setTemperatureOfAllElementsToZero(p); p[V/2][H/2] = 1; /* set temperature of source element */ simulateHeatDiffusion(1000, p); displayHeatDistribution(p); } /* heatSupport.h */ /* number of rows (V) and columns (H) of elements */ #define V 20 #define H 30 /* set element temperatures to zero */ void setTemperatureOfAllElementsToZero(double p[V][H]); /* simulate heat diffusion on p for given number of time steps */ void simulateHeatDiffusion(int numberOfTimeSteps, double p[V][H]); /* display on screen the temperature distribution of p */ void displayHeatDistribution(double p[V][H]); /* heatSupport.c */ #include "heatSupport.h" #include "heatSupportPrivate.h" #include "numericUtilities.h" #include #include void setTemperatureOfAllElementsToZero(double p[V][H]) { int i, j; for (i = 0; i < V; i = i + 1) { for (j = 0; j < H; j = j + 1) { p[i][j] = 0; } } } void simulateHeatDiffusion(int numberOfTimeSteps, double p[V][H]) { int t; for (t = 0; t < numberOfTimeSteps; t = t + 1) { updateInteriorTemperatures(p); } } void displayHeatDistribution(double p[V][H]) { int i, j; for (i = 0; i < V; i = i + 1) { for (j = 0; j < H; j = j + 1) { printf("%c", digitalDisplay(" -+*", 0, 1, p[i][j])); } printf("\n"); } } /* heatSupportPrivate.h */ /* update temperatures of interior elements of p */ void updateInteriorTemperatures(double p[V][H]); /* heatSupportPrivate.c */ #include "heatSupport.h" #include "heatSupportPrivate.h" void updateInteriorTemperatures(double p[V][H]) { int i, j; double sourceElementTemperature; sourceElementTemperature = p[V/2][H/2]; for (i = 1; i < V - 1; i = i + 1) { for (j = 1; j < H - 1; j = j + 1) { p[i][j] = (p[i-1][j] + p[i][j+1] + p[i+1][j] + p[i][j-1])/4; } } p[V/2][H/2] = sourceElementTemperature; /* restore source element */ } /* numericUtilities.h */ /* deliver the larger of two integers */ int maxInt(int x, int y); /* deliver the smaller of two integers */ int minInt(int x, int y); /* digitize x according to a uniform partition of the range (a, b) into n digital levels assuming a < x < b truncating out-of-range x's to appropriate exteme */ int digitize(int n, double a, double b, double x); /* deliver digital representation of x selected from a given sequence of display characters assuming a < x < b truncating out-of-range x's to appropriate exteme */ char digitalDisplay(char displayChars[ ], double a, double b, double x); /* numericUtilities.c */ #include "numericUtilities.h" #include int maxInt(int x, int y) { return(x < y ? y : x); } int minInt(int x, int y) { return(-maxInt(-x, -y)); } int digitize(int n, double a, double b, double x) { return(maxInt(0, minInt(n - 1, n*(x - a)/(b - a)))); } char digitalDisplay(char displayChars[ ], double a, double b, double x) { return(displayChars[digitize(strlen(displayChars), a, b, x)]); }