HotPlate simulation

Part 2 - Update Elements Once (25 points)

The job of the algorithm is to find the steady-state temperature distribution of the interior elements which are constantly changing to become the average of the temperature of their neighbors. Steady state means that the temperature of any cell is virtually unchanging and approximately equal to the average of the temperatures of its four neighbors on the north, south, east, and west directions (do not worry about the diagonal neighbors). Note that the steady state temperatures will be the same regardless of what the beginning temperatures are for the interior elements.

To calculate the new temperature value of an array element, take the average of the values in the 4 neighbors of that cell from the previous iteration. This value will be placed in the output array.

For example, suppose the current state of the plate is (it won't be like this to start out with, but suppose it were):

0.0000, 100.0000, 100.0000, 100.0000, 0.0000
0.0000, 40.6250, 50.0000, 40.6250, 0.0000
0.0000, 25.0000, 31.2500, 25.0000, 0.0000
0.0000, 40.6250, 50.0000, 40.6250, 0.0000
0.0000, 100.0000, 100.0000, 100.0000, 0.0000

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;
   
//Print the numbers in the array with a fixed precision of 4 and width 10.   
const int SIZE_OF_ARRAY = 20;
const double CHANGE = 0.1;
const double NEIGHBORS = 4.0;
double averageTemperature = 0.0;
double largestChange = 0.0;
double oldValue = 0.0;

void initial(double hotPlate[SIZE_OF_ARRAY][SIZE_OF_ARRAY]){
	for (int row = 0; row < SIZE_OF_ARRAY; row++)
	{
		for (int colomn = 0; colomn < SIZE_OF_ARRAY; colomn++)
		{
			if (row == 0 || row == 19)
			{
				if (colomn == 0 || colomn == 19)
				{
					hotPlate[row][colomn] = 0;
				}
				else
				{
					hotPlate[row][colomn] = 100;
				}
			}
			else
			{
				hotPlate[row][colomn] = 0;
			}
		}
	}
}

void print(double hotPlate[SIZE_OF_ARRAY][SIZE_OF_ARRAY]){
	for (int row = 0; row < SIZE_OF_ARRAY; row++)
	{
		for (int colomn = 0; colomn < SIZE_OF_ARRAY; colomn++)
		{
		   if (colomn == 19) {
			cout << fixed << setprecision(4) << setw(10) << hotPlate[row][colomn];
		   }
		   else {
		      cout << fixed << setprecision(4) << setw(10) << hotPlate[row][colomn] << ",";
		   }
		}
		cout << endl;
	}
}

void firstIteration(double hotPlate[SIZE_OF_ARRAY][SIZE_OF_ARRAY])
{
	for (int row = 1; row < SIZE_OF_ARRAY - 1; row++)
	{
		for (int column = 1; column < SIZE_OF_ARRAY - 1; column++)
		{

			averageTemperature = (hotPlate[row][column - 1] + hotPlate[row + 1][column] + hotPlate[row][column + 1] + hotPlate[row - 1][column]) / NEIGHBORS;
			hotPlate[row][column] = averageTemperature;

		}
	}

}

void average(double hotPlate[SIZE_OF_ARRAY][SIZE_OF_ARRAY])
{
	do
	{
		largestChange = 0;
		for (int row = 1; row < SIZE_OF_ARRAY - 1; row++)
		{
			for (int column = 1; column < SIZE_OF_ARRAY - 1; column++)
			{
				double oldValue = hotPlate[row][column];
				averageTemperature = (hotPlate[row][column - 1] + hotPlate[row + 1][column] + hotPlate[row][column + 1] + hotPlate[row - 1][column]) / NEIGHBORS;
				hotPlate[row][column] = averageTemperature;

				double change = abs(oldValue - hotPlate[row][column]);
				if (change > largestChange)
				{
					largestChange = change;
				}
			}
		}


	} while (largestChange > CHANGE);

}
   
 int main() {  
    double hotPlate[SIZE_OF_ARRAY][SIZE_OF_ARRAY];
cout << "Hotplate simulator" << endl;
cout << endl;
cout << "Printing initial plate..." << endl;
	initial(hotPlate);
	print(hotPlate);
	cout << endl ;
	firstIteration(hotPlate);
cout << "Printing plate after one iteration..." << endl;
	print(hotPlate);

	cout << endl << endl;

	average(hotPlate);
	print(hotPlate);


 return 0;  
}
I do not see any question.


You cannot update array[x] right after computing its new value because if you do, then computing array[x+1]'s new value uses "future values".

You do need two arrays: current state and next state. You fill the next state array based on the values in the current state array. When complete, the next state becomes current state.
Topic archived. No new replies allowed.