Write a program that calculates the varying internal temperature of an object. The object’s shape is a rectangle and it is subject to fixed temperatures at its 4 edges. The initial temperature of the interior will not matter and can be set to any value like 0. We are interested in the final or steady state internal temperatures.

14 10
90 90 90 90 90 90 90 90 90 90
89 0 0 0 0 0 0 0 0 85
88 0 0 0 0 0 0 0 0 85
86 0 0 0 0 0 0 0 0 80
84 0 0 0 0 0 0 0 0 80
82 0 0 0 0 0 0 0 0 80
79 0 0 0 0 0 0 0 0 75
76 0 0 0 0 0 0 0 0 75
73 0 0 0 0 0 0 0 0 70
70 0 0 0 0 0 0 0 0 70
67 0 0 0 0 0 0 0 0 67
66 0 0 0 0 0 0 0 0 67
62 0 0 0 0 0 0 0 0 67
58 58 58 58 58 58 58 60 62 64

The interior of the object (denoted by zeros) is arbitrarily sectioned off into small cells. We will determine the temperature of these small cells. Since the outside edges are fixed temperatures finding the inside temperatures can easily be done by repeatedly averaging the temperatures surrounding each cell. For example:
cell[3,4] = ( cell[3,3] + cell[3,5] + cell[2,4] + cell[4,4]) / 4
Each cell must be recalculated many times until it does not change or by much.

**That is what the program is suppose to do **

///////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;

const int MAX_ROWS = 20, MAX_COLS = 20;

float finiteElementAnalysis (float temperature[][MAX_COLS], int rows, int cols, float &sum); //Prototype
void display (const float temperature[][MAX_COLS], int rows, int cols); //Prototype
void readfile (float temperature[][MAX_COLS], int& rows, int& cols); //Prototype
void outputdisplay (const float temperature[][MAX_COLS], int rows, int cols); //Prototype

///////////////////////////////////////////Main Program Start//////////////////////////////////////////////////

int main (void)
{

float temperature[MAX_ROWS][MAX_COLS];
int numberOfRows = MAX_ROWS;
int numberOfCols = MAX_COLS;
float sum = 10; //initiate to any number higher than iterative control statement test value. this assumes that after the first cycle of calculations, the sum of the differences will be to big. this allows us to create a variable to pass into our function, that I can then assign to be =0 for logical reasons.
int count = 0; //I need to count how many times my iterative loop runs for displaying in the output.

display (temperature, numberOfRows, numberOfCols);
outputdisplay (temperature, numberOfRows, numberOfCols);

while (sum >= .01) //this will call a function repetedly until the function returns a value that is less than .01 for the sum variable. I finally understand that you could test the amount of time it would take for temperatures to stabilize if we knew all the complex details like heat capacitance of the material. I struggled with this concept until this lab.
{
finiteElementAnalysis (temperature, numberOfRows, numberOfCols, sum);//call a function that calculates each cells temperature one time, as well as the sum of the absolute value of the difference between the old cell value and the new cell value
count++; //this will keep track of how many times the finiteElementAnalysis function is called, aka number of loops
}

display (temperature, numberOfRows, numberOfCols);
outputdisplay (temperature, numberOfRows, numberOfCols);

return (0);
}

///////////////////////////////////////////Main Program End//////////////////////////////////////////////////

// Calculates each cell’s new value just once. Also keeps track of the sum of the absolute value of the differences before and after the changes happen
// input: array, number of rows, number of columns, float by reference called sum.
// output: array, sum of fabs(cell differences).
float finiteElementAnalysis (float temperature[][MAX_COLS], int rows, int cols, float &sum)
{
const int ROWS = 14;
const int COLS = 10;
float copyTemperature[ROWS][COLS];
sum = 0;
for (int count=1; count < 13; count++) //for each row, as long as the rows are less than the number of rows in the array
{
for(int colCount=1; colCount < 9; colCount++ ) //do, for each column within that row as long as the rows are less than the number of rows in the array
{
copyTemperature[count][colCount]=temperature[count][colCount];
//do, this
temperature[count][colCount]= (temperature[count-1][colCount]+temperature[count+1][colCount]+temperature[count][colCount+1]+temperature[count][colCount-1])/4;
//calculates the new temperature of the cell, based on averaging its surrounding cells and assigning that new value to the cell.

sum = sum + fabs (temperature[count][colCount]-copyTemperature[count][colCount]);

}

}
return temperature[rows][cols], sum;
}
//////////////////////////////////////////////////////
// Function to display a second array of numbers.
// input: array, number of rows, number of columns.
// output: none
void display (const float temperature[][MAX_COLS], int rows, int cols)
{
ofstream outputFile ("Temperatures.out", ios::app); //opens temperatures.out file and notes to add from where we left off

for (int count = 0; count < rows; count++) //while the count is less than the number of rows, increment the count, and:
{
for (int colCount =0; colCount < cols; colCount++) //while each colcount is less than the number of cols, increment the colcount, and:
{
outputFile << right << setw(3) << floor ( temperature[count][colCount] ); //output each value in the array into a table, right justified and rounded off to the largest integer not greater than the values in the array
}
outputFile <<endl;

}
outputFile.close();

}

//this function displays the array on the screen
void outputdisplay (const float temperature[][MAX_COLS], int rows, int cols)
{

for (int count = 0; count <rows; count++)
{
for (int colCount =0; colCount <cols; colCount++)
{
cout << right << setw(3) << floor (temperature[count][colCount]);
}
cout <<endl;
}

}

//////////////////////////////////////////////////////
// code to read a file representing internal temperatures
// of an object. The first 2 numbers represent the number
// of rows then columns.
// Inputs: none.
// Outputs: array, number of rows, number of columns.
void readfile (float temperature[][MAX_COLS], int& rows, int& cols)
{
ifstream inputfile ("temp_input.txt");
inputfile>>rows;
inputfile>>cols;
for (int count = 0; count <rows; count++)
{
for (int colCount =0; colCount <cols; colCount++)
{
inputfile>>temperature[count][colCount]; //INPUT each row into array
}
}
}

void afterLoopHeader (int count) //this function saves the header to the file, by adding on to it (ios::app)
{
ofstream outputFile ("Temperatures.out", ios::app);

outputFile<< endl <<"After: "<< count <<" loops"<<endl<<endl;
outputFile.close();
}
void outputAfterLoopHeader (int count) //this prints to screen as a header indicating the number of loops required
{
cout<< endl <<"After: "<< count <<" loops"<<endl<<endl;
}

void preLoopHeader () //this runs before calling the function that displays the array, saves to file
{
ofstream outputFile ("Temperatures.out");

outputFile << endl << "Before...."<<endl<<endl;
outputFile.close();
}

void outputPreLoopHeader () //this runs before calling the function that displays the array, displays to screen
{
cout << endl << "Before...."<<endl<<endl;

}
Last edited on
It compiled after I added `#include <cmath> ` Not sure if there is anything else wrong...

I'm feeling a little tired tonight, and my telepathy isn't working as well is it normally does. Perhaps you would be kind enough to, y'know, actually tell us what the problem you're having with your code is?
It compiles without a problem it infinitely loops, and not producing the data it is suppose to. The instructions of what it is suppose to do is at the top of the program.
`return temperature[rows][cols], sum;`