The temperature distribution in a thin metal plate with cons
Solution
Here is the code for you:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double Array[35][20], left, right, top, bottom, temp, epsilon;
int ROWS, COLS, count = 0;
bool equilibrium = false;
cout<<\"Enter the size of the plate ROWSxCOLS: \";
cin>>ROWS;
cin>>COLS;
cout<<\"Enter the temperature at the top edge: \";
cin>>top;
cout<<\"Enter the temperature at the bottom edge: \";
cin>>bottom;
cout<<\"Enter the temperature at the left edge: \";
cin>>left;
cout<<\"Enter the temperature at the right edge: \";
cin>>right;
for(int i = 0; i < ROWS; i++) //Initializing all the array values to 0.
for(int j = 0; j < COLS; j++)
Array[i][j] = 0;
for(int i = 1; i < COLS-1; i++) //Initializing top edge.
Array[0][i] = top;
for(int i = 1; i < ROWS-1; i++) //Initializing left edge.
Array[i][0] = left;
for(int i = 1; i < COLS-1; i++) //Initializing bottom edge.
Array[ROWS-1][i] = bottom;
for(int i = 1; i < ROWS-1; i++) //Initializing right edge.
Array[i][COLS-1] = right;
Array[0][0] = (top + left) / 2; //Calculating corner values.
Array[0][COLS-1] = (top + right) / 2;
Array[ROWS-1][0] = (left + bottom) / 2;
Array[ROWS-1][COLS-1] = (bottom + right) / 2;
cout<<\"Enter the epsilon value: \"; //Read epsilon value.
cin>>epsilon;
cout<<\"Initial temperatures in the array: \"<<endl; //Print initial temperatures.
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
cout<<fixed<<setprecision(2)<<Array[i][j]<<\" \";
cout<<endl;
}
while(!equilibrium) //Modifying the temperatures, and running the loop.
{
equilibrium = true;
for(int i = 1; i < ROWS-1; i++)
for(int j = 1; j < COLS-1; j++)
{
temp = (Array[i-1][j] + Array[i+1][j] + Array[i][j-1] + Array[i][j+1]) / 4;
if(abs(temp - Array[i][j]) > epsilon)
equilibrium = false;
Array[i][j] = temp;
}
count++;
}
cout<<\"The plate reached equilibrium after \"<<count<<\" iterations.\"<<endl;
cout<<\"Final temperatures in the array: \"<<endl; //Print final temperatures.
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
cout<<setw(5)<<fixed<<setprecision(2)<<Array[i][j]<<\" \";
cout<<endl;
}
}

