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
|
#include <iostream>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;
const int MAX_TEMPERATURE = 100;
const int MIN_TEMPERATURE = 0;
const int ROWS = 20;
const int COLUMNS = 20;
const int DIMENSIONS = 20;
bool update = true;
const double CHANGE = .1;
const int NEIGHBORS = 4;
void printHotPlate(double(&plate)[ROWS][COLUMNS], int dimension){
for (int row = 0; row < dimension; row++){
for (int column = 0; column < dimension; column++){
if (column == 19){
cout << fixed << setprecision(4) << setw(10) << plate[row][column];
}
else{
cout << fixed << setprecision(4) << setw(10) << plate[row][column] << ",";
}
}
cout << endl;
}
}
void initializeHotPlate(double(&plate)[ROWS][COLUMNS], int dimension, int MIN_TEMPERATURE, int MAX_TEMPERATURE){
for (int row = 0; row < dimension; row++){
for (int column = 0; column < dimension; column++){
if (row == 0 || row == 19){
if (column == 0 || column == 19){
plate[row][column] = MIN_TEMPERATURE;
}
else{
plate[row][column] = MAX_TEMPERATURE;
}
}
else{
plate[row][column] = 0;
}
}
}
}
void averageHotPlate(double(&oldPlate)[ROWS][COLUMNS], double(&plate)[ROWS][COLUMNS], bool& update, int dimension, double CHANGE, int NEIGHBORS){
update = false;
for (int row = 1; row < dimension - 1; row++){
for (int column = 1; column < dimension - 1; column++){
plate[row][column] = (oldPlate[row + 1][column] + oldPlate[row - 1][column] + oldPlate[row][column + 1] + oldPlate[row][column - 1]) / NEIGHBORS;
if (abs(plate[row][column] - oldPlate[row][column]) > CHANGE){
update = true;
}
}
}
for (int row = 1; row < dimension - 1; row++){
for (int column = 1; column < dimension - 1; column++){
oldPlate[row][column] = plate[row][column];
}
}
}
void exportHotPlate(double(&plate)[ROWS][COLUMNS], int dimension){
ofstream out_file;
out_file.open("lab6output.csv");
for (int row = 0; row < dimension; row++){
for (int column = 0; column < dimension; column++){
if (column == 19){
cout << fixed << setprecision(4) << plate[row][column];
}
else{
cout << fixed << setprecision(4) << plate[row][column] << "\t";
}
}
cout << endl;
}
}
int main()
{
const int MAX_TEMPERATURE = 100;
const int MIN_TEMPERATURE = 0;
const int ROWS = 20;
const int COLUMNS = 20;
const int DIMENSIONS = 20;
bool update = true;
const double CHANGE = .1;
const int NEIGHBORS = 4;
double oldPlate[ROWS][COLUMNS];
double plate[ROWS][COLUMNS];
cout << "Hotplate simulator" << endl;
cout << endl;
cout << "Printing initial plate..." << endl;
initializeHotPlate(oldPlate, DIMENSIONS, MIN_TEMPERATURE, MAX_TEMPERATURE);
initializeHotPlate(plate, DIMENSIONS, MIN_TEMPERATURE, MAX_TEMPERATURE);
printHotPlate(oldPlate, DIMENSIONS);
cout << endl;
cout << "Printing plate after one iteration..." << endl;
averageHotPlate(oldPlate, plate, update, ROWS, CHANGE, NEIGHBORS);
printHotPlate(plate, DIMENSIONS);
cout << endl<< endl;
cout << "Printing final plate..." << endl;
while (update){
averageHotPlate(oldPlate, plate, update, DIMENSIONS, CHANGE, NEIGHBORS);
}
printHotPlate(plate, DIMENSIONS);
cout << endl << endl;
exportHotPlate(plate, DIMENSIONS);
return 0;
}
|