You are on page 1of 4

/*Robin Luo

CMPSC 201 F15


Project 4 Section 3
Purpose: Program to initialize a two-dimensional array (representing a thin meta
l plate of four edges, each of which is maintained at a constant temperature),
where the size of the array and the values in the top, bottom, left and right ed
ges of the array (minus the four corners) are given by the user, and the four
corners of the array are calculated by averaging the two corresponding edge, and
then continuously calculate the "interior" (all values that are not the values
in the four edges or the four corners) values starting with the top row and goin
g from left to right and then starting in the next row down until the interior
values in the array reach equilibrium (where the change in the values at all spo
ts in the array between two iterations of calculating the interior values are
less than epsilon, a value given by the user). A function is also created in thi
s program in order to output the array once it reaches equilibrium, and at the
end of the main function, the array is outputted.
Inputs: the number of rows in the two-dimensional array, the number of columns,
the values in the top edge, the values in the bottom edge, the values in the
right edge, the values in the left edge, and epsilon
Outputs: all of the prompts asking the user for the information in the "Inputs"
section, the array once it reaches equilibrium, and the number of iterations
that had to be run in order for the array to reach equilibrium.
Process: Ask the user to input the number of rows in the two-dimensionsal array,
the number of columns in the two-dimensional array, the values in the top edge,
the values in the bottom edge, the values in the right edge, the values in the l
eft edge, and epsilon. Use a nested for loop to create a two-dimensional array
and set all values in the array to be zero. Use two separate single for loops to
input the values in the top and bottom edges and in the right and left edges
into the two-dimensional array. Now, use a nested for loop inside a do-while loo
p to calculate the interior values and continue to calculate the interior
values until the difference between the value in the spot of the array at one it
eration and the value in that same spot at the next iteration is less than
epsilon. Once the do-while loop is finished running, output the array using a fu
nction that is created which uses a nested for loop to output values in the
two-dimensional array.
*/
# include <iostream> //preprocessor directive for input/output
# include <iomanip>
using namespace std;
void displayArray(double[][20], int, int);
int main()
{
int rows, cols, count = 0;
double topEdge, bottomEdge, leftEdge, rightEdge, epsilon;
double plate[35][20], plateDiffs[35][20];
//Prompt the user to enter the size of the 2-D array and check that the
values entered are valid
cout << "Today, we will be using a 2-dimensional array of square cells t
o model a thin metal plate containing 4 edges, each of which is maintained at a
constant temperature. ";
cout << "What size would you like our 2-dimensional array to be? Please

enter the number of rows first. The number of rows must be at least 3 and at mos
t 35. ";
cin >> rows;
while (rows < 3 || rows > 35)
{
cout << "The number of rows in our array must be at least 3 and
at most 35. Please enter a valid number for the rows in our array. ";
cin >> rows;
}
cout << "Please enter the number of columns you would like our array to
have. The number of columns must be at least 3 and at most 20. ";
cin >> cols;
while (cols < 3 || cols > 20)
{
cout << "The number of columns in our array must be at least 3 a
nd at most 30. Please enter a valid number for the columns in our array. ";
cin >> cols;
}
//Prompt the user to enter the values of the top, bottom, left, and righ
t edges of the 2-D array and check that the values entered are valid
cout << "What temperature would you like the top edge of the thin metal
plate to be ? The temperature must be at least 0 and at most 300. ";
cin >> topEdge;
while (topEdge < 0 || topEdge > 300)
{
cout << "The temperature of the top edge must be at least 0 and
at most 300. Please enter a valid number for the temperature of the top edge. ";
cin >> topEdge;
}
cout << "What temperature would you like the bottom edge of the thin met
al plate to be ? The temperature must be at least 0 and at most 300. ";
cin >> bottomEdge;
while (bottomEdge < 0 || bottomEdge > 300)
{
cout << "The temperature of the bottom edge must be at least 0 a
nd at most 300. Please enter a valid number for the temperature of the bottom ed
ge. ";
cin >> bottomEdge;
}
cout << "What temperature would you like the left edge of the thin metal
plate to be ? The temperature must be at least 0 and at most 300. ";
cin >> leftEdge;
while (leftEdge < 0 || leftEdge > 300)
{
cout << "The temperature of the left edge must be at least 0 and
at most 300. Please enter a valid number for the temperature of the left edge.
";
cin >> leftEdge;
}
cout << "What temperature would you like the right edge of the thin meta
l plate to be ? The temperature must be at least 0 and at most 300. ";
cin >> rightEdge;
while (rightEdge < 0 || rightEdge > 300)
{
cout << "The temperature of the right edge must be at least 0 an
d at most 300. Please enter a valid number for the temperature of the right edge
. ";
cin >> rightEdge;
}

//Prompt the user to enter a value for epsilon


cout << "Please enter a value for epsilon. Since the interior temperatur
es of the thin metal plate will continue to update by averaging the temperatures
immediately above, below";
cout << ", to the left, and to the right, the interior temperatures will
reach equilibrium and stop updating once the change between two temperatures in
one position is less than";
cout << " the value of epsilon. ";
cin >> epsilon;
//Construct the exterior values of the array and set all interior values
to 0
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
plate[i][j] = 0;
}
}
for (int j = 1; j < cols - 1; j++)
{
plate[0][j] = topEdge;
plate[rows - 1][j] = bottomEdge;
}
for (int i = 1; i < rows - 1; i++)
{
plate[i][0] = leftEdge;
plate[i][cols - 1] = rightEdge;
}
plate[0][0] = 0.5*(plate[0][1] + plate[1][0]);
plate[0][cols - 1] = 0.5*(plate[0][cols - 2] + plate[1][cols - 1]);
plate[rows - 1][cols - 1] = 0.5*(plate[rows - 2][cols - 1] + plate[rows
- 1][cols - 2]);
plate[rows - 1][0] = 0.5*(plate[rows - 1][1] + plate[rows - 2][0]);
//Calculate the interior values of the array, and continue to update the
calculation of the interior values of the array until the change in a position
in the array is less than epsilon
double difference = 0, preChange, postChange;
int epsCount = 0, m, n;
do
{
for (int i = 1; i < rows - 1; i++)
{
for (int j = 1; j < cols - 1; j++)
{
preChange = plate[i][j];
plate[i][j] = 0.25*(plate[i][j + 1] + plate[i +
1][j] + plate[i - 1][j] + plate[i][j - 1]);
postChange = plate[i][j];
difference = abs(preChange - postChange);
plateDiffs[i][j] = difference;
}
}
for (m = 1; m < rows - 1; m++)
{
for (n = 1; n < cols - 1; n++)
{

if (plateDiffs[m][n] < abs(epsilon))


epsCount++;
}
}
count++;
} while (epsCount < m*n);
//Output the array
cout << endl;
if (count == 1)
cout << "The plate reached equilibrium after 1 iteration." << en
dl << "The equilibrium temperatures are:" << endl;
else
cout << "The plate reached equilibrium after " << count << " ite
rations." << endl << "The equilibrium temperatures are:" << endl;
displayArray(plate, rows, cols);
return 0;
}
//function used to display the 2-D array
void displayArray(double p[][20], int r, int c)
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cout << fixed << showpoint << setprecision(1) << setw(7)
<< p[i][j];
}
cout << endl;
}
}

You might also like