Help with hot plate

Hello,
For my class we are doing a hotplate lab. The part I am having trouble with is reading in a file called Inputplate.txt, and using the information in that file to update my current array. Here is what the specs of the lab say to do.

Part 5 - Input Plate
Input into your plate array from the file "Inputplate.txt" which you should have in the same directory as "Hotplate.csv" in VS. For zyBooks we have already uploaded a file "Inputplate.txt" which you can open and read from. Fill ALL elements of your array from this file including the borders, (though the borders will continue to have the same static values as before). After inputting the initial array, iterate just 3 times with your plate update loop, and then output the current plate values to the screen. These will be the array values after exactly 3 update iterations. You may assume that the input file has only doubles and correctly matches the 10x10 array, thus you will not need to error check your file input. Note that regardless of initial temperatures in the interior, if you have the same steady edge temperatures, and if you run until stable, this plate would have the same stable values (within .1 degrees) as the stable plate above. You might want to try it. Download the contents of the input file here and put a copy in your VS Hot Plate directory with the name "Inputplate.txt".

So far for my code I have been able to input the Inputplate.txt file, and I've tried to create a for loop in my int main that iterates 3 times. However when I output my new array with the input from the Inputplate.txt, I have random garbage that comes out. I am what I am doing wrong in my code.
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;

const int SIZE = 10; // 10 original
const double CHANGE = 0.1;
const double NEIGHBORS = 4.0;
const int LEFT = 0;
const int RIGHT = 9;
const int WIDTH = 10;
const int DECIMAL_PRECISION = 4;
string filename = "Inputplate.txt";

void Initial(double hot_plate[][SIZE])
{
	for (int row = 0; row < SIZE; row++)
	{
		for (int col = 0; col < SIZE; col++)
		{
			if (row == LEFT || row == RIGHT) // row == 9 original
			{
				if (col == LEFT || col == RIGHT) // row == 0 original
				{
					hot_plate[row][col] = 0;
				}
				else
				{
					hot_plate[row][col] = 100;
				}
			}
			else
			{
				hot_plate[row][col] = 0;
			}
		}
	}
}

void Print(double hot_plate[][SIZE])
{
	for (int row = 0; row < SIZE; row++)
	{
		for (int col = 0; col < SIZE; col++)
		{
			cout << fixed << setprecision(DECIMAL_PRECISION) << setw(WIDTH) << hot_plate[row][col];
			if (col < SIZE - 1) {
				cout << ",";
			}
		}
		cout << endl;
	}
}

void FirstAverage(double hot_plate[][SIZE], double copy_hot_plate[][SIZE])
{
	
	double average_temp = 0.0;
	int row = 0;
	int col = 0;

	for (int row = 1; row < SIZE - 1; row++)
	{
		for (int col = 1; col < SIZE - 1; col++)
		{
			average_temp = (hot_plate[row - 1 ][col] + hot_plate[row + 1][col] + hot_plate[row][col - 1] + hot_plate[row][col + 1]) / NEIGHBORS;
			copy_hot_plate[row][col] = average_temp;

		}

	}




}


void MyAverage(double hot_plate[][SIZE], double copy_hot_plate[][SIZE]) {

	bool keepgoing = false;
	const double CHANGE = 0.1;
	const double NEIGHBORS = 4.0;

	do {
		keepgoing = false;
		for (int row = 1; row < SIZE - 1; row++) {
			for (int column = 1; column < SIZE - 1; column++) {

				copy_hot_plate[row][column] = (hot_plate[row + 1][column] + hot_plate[row - 1][column] + hot_plate[row][column + 1] + hot_plate[row][column - 1]) / NEIGHBORS;
				if (abs(hot_plate[row][column] - copy_hot_plate[row][column]) > CHANGE) {
					keepgoing = true;
				}
			}
		}
		for (int row = 1; row < SIZE - 1; row++) {
			for (int column = 1; column < SIZE - 1; column++) {
				hot_plate[row][column] = copy_hot_plate[row][column];
			}
		}
		//Print(hot_plate);
	} while (keepgoing);
	
}

void SaveFile(double hot_plate[][SIZE])
{
	ofstream out;
	out.open("Hotplate.csv");

	for (int row = 0; row < SIZE; row++)
	{
		for (int col = 0; col < SIZE; col++)
		{
			out << fixed << fixed << setprecision(DECIMAL_PRECISION) << setw(WIDTH) << hot_plate[row][col];
			if (col < SIZE - 1) {
				out << ",";
				}
			
	
		}
		out << endl;
		
	}

	out.close();
}

//void ReadFile(double hot_plate[][SIZE])
//{
//
//}

int main()
{
	double hot_plate[SIZE][SIZE];
	double copy_hot_plate[SIZE][SIZE];
	bool keepgoing = false;
	const double CHANGE = 0.1;
	const double NEIGHBORS = 4.0;
	
	cout << "Hotplate simulator" << endl << endl;
	cout << "Printing initial plate..." << endl;
	Initial(hot_plate);
	Initial(copy_hot_plate);
	Print(hot_plate);

	cout << endl << endl;

	cout << "Printing plate after one iteration..." << endl;
	FirstAverage(hot_plate, copy_hot_plate);
	Print(copy_hot_plate);
	
	cout << endl << endl;

	cout << "Printing final plate..." << endl;
	MyAverage(hot_plate, copy_hot_plate);
	Print(hot_plate);

	cout << endl << "Outputting final plate to file \"Hotplate.csv\"" << endl;
	SaveFile(hot_plate);
	//ReadFile(hot_plate);

	ifstream in;
	in.open(filename);

	cout << endl << "Printing input plate after 3 updates..." << endl;
	for (int k = 1; k < 3; k++) {
		keepgoing = false;
		for (int row = 1; row < SIZE - 1; row++) {
			for (int column = 1; column < SIZE - 1; column++) {

				copy_hot_plate[row][column] = (hot_plate[row + 1][column] + hot_plate[row - 1][column] + hot_plate[row][column + 1] + hot_plate[row][column - 1]) / NEIGHBORS;
				if (abs(hot_plate[row][column] - copy_hot_plate[row][column]) > CHANGE) {
					keepgoing = true;
				}
			}
			for (int row = 1; row < SIZE - 1; row++) {
				for (int column = 1; column < SIZE - 1; column++) {
					hot_plate[row][column] = copy_hot_plate[row][column];
				}
			}
			Print(copy_hot_plate);
		}
	}

	system("pause");
	return 0;
}
The code you have above runs and gives a plausible answer.

So your problem almost certainly stems from your ReadFile() routine ...

... which is the one bit of code you haven't included!!!


With this input routine that you presumably have somewhere:
(a) check (from the state of the stream) whether the file can be found and is actually open;
(b) print out the matrix immediately after reading it; then you can identify whether you have read everything correctly before applying your Jacobi algorithm.
Topic archived. No new replies allowed.