Question: Basic algorithm: Get from the user the names of the input and output files. Read from the input file the initial temps for top, right,
Basic algorithm:
- Get from the user the names of the input and output files.
- Read from the input file the initial temps for top, right, bottom, and left sides of plate.
- Read from the input file the tolerance for equilibrium.
- Initialize the edges of the 2D grid with initial temps you got from the input file, and initialize the inner cells of the grid to 0.0.
- Continue updating temperature values within inner cells until equilibrium is reached.
- Output to the output file the values of the inner cells of the grid after equilibrium obtained.
Step 5 details
- When updating the grid's temperatures always start at the top, left inner cell and update that row before updating the row below it. So, the first cell to be updated will be grid[1][1], then grid[1][2], and so on.
- Never change an edge cell's temperature.
- A cell's new temperature will always be the average of the 4 cells adjacent to it, the cells immediately above, below, left, and right of the cell.
- You should update all inner cells of the entire grid, while always keeping track of the largest difference between the old value and the new calculated value for a cell. Once you have completely updated the grid, if your max difference is within the tolerance level, equilibrium has been reached. You are done. Go ahead and output the temperatures in each cell of your grid. If not, you need to update the entire grid again.
- You are required to use 2D arrays, NOT vectors, for this assignment.
- The grid dimensions will always be the same as the grid example above (6 X 8).
Input/Output File Specifications
Input and output file names come from the user. Ask for input file first.
Input File
The input file will have the 4 starting temps on one line, each separated by a space and the tolerance on the second line.
29 45 0 15
0.5
Output File
When completed, the output file should list the temperatures of the inner cells only:
20.6404 22.6151 23.944 25.7453 28.7711 34.1826
16.4471 17.5668 19.1289 21.8528 26.5254 34.0902
13.1644 12.8385 13.9106 16.6151 21.7503 30.7639
8.81194 7.2602 7.58734 9.35255 13.3295 22.2734
my code is giving me the wrong output and I don't know why, i keep getting 4.63652e+265 5.46709e+265 4.34022e+265 2.86671e+265 1.64628e+265 7.34833e+264 1.3079e+266 1.28916e+266 9.02707e+265 5.48035e+265 2.98356e+265 1.29305e+265 3.47878e+266 2.39934e+266 1.33961e+266 7.04404e+265 3.51457e+265 1.45382e+265 1.02079e+267 3.4898e+266 1.35198e+266 5.78518e+265 2.57685e+265 1.00767e+265
here is my code:
#include//input that reads data #include//need for files #include//dynamic memory managemen #include//for setprecision #include#include using namespace std; void initMatrix(double plate[6][8], //IN - structuring Numbers from the grid double &top, //IN - Numbers from the top of grid double &right, //IN - Numbers from the right of grid double &bottom, //IN - Numbers from the bottom of grid double &left); //IN - Numbers from the left of grid double updateGrid(double grid[6][8]); //IN - updated numbers from files int main(){ //somewhat like a mini diagram double table[6][8]; //CAL - The grid length and height table[6][8] = {0}; double top; //CAL - top of the grid top = 0.0; double right; //CAL - right of the grid right = 0.0; double left; //CAL - left of the grid left = 0.0; double bottom; //CAL - Bottom of the grid bottom = 0.0; double tolerance; //CAL - Tha amount a value can change tolerance = 0.0; ifstream inputfile; //CAL - open files ofstream oufile; //CAL - open files string infile; //IN & CAL - first string to open files infile = ""; string outfile; //IN & CAL - second string to open files outfile =""; //user enter input file name cout << "Enter input file name: "; cin >> infile; cout << endl; //prompt user to enter the output file name cout << " Enter output file name: "; cin >> outfile; cout << endl; cout << endl; //define input file path inputfile.open(infile); if (!inputfile.is_open()) { cout << "Could not open file " << infile << endl; return 0; } //define output file path oufile.open(outfile); if(!oufile.is_open()){ cout << "Could not open file " << outfile << endl; return 0; } //gaining the numbers from the file inputfile >> top; inputfile >> right; inputfile >> bottom; inputfile >> left; inputfile >> tolerance; //initialize the grid matrix initMatrix(table,top,right,bottom,left); double variable; //CAL - hold number and compare do{ variable = updateGrid(table); } while(variable > tolerance); for(int i = 1; i < 5; i++){ for(int j = 1; j < 7; j++){ cout << table[i][j] << " "; } cout << endl; } return 0; } void initMatrix(double plate[6][8], //IN - structuring Numbers from the grid double&top, //IN - Numbers from the top of grid double&right, //IN - Numbers from the right of grid double&bottom, //IN - Numbers from the bottom of grid double&left){ //IN - Numbers from the left of grid for(int top1 = 0; top1 < 8; top1++){ plate[0][top1] = top; } for(int right1 = 1; right1 < 5; right1++){ plate[right1][7] = right; } for(int bottom1 = 5; bottom1 < 8; bottom1++){ plate[5][bottom1] = bottom; } for(int left1 = 1; left1 < 5; left1++){ plate[left1][0] = left; } } double updateGrid(double grid[6][8]){ //IN - updated numbers double initial; initial = 0; double different; different = 0; double maxDifference; maxDifference = 0; for(int i = 1; i < 5; i++){ for(int j = 1; j < 7; j++){ initial = grid[i][j]; grid[i][j] = ((grid[i-1][j] + grid[i][j-1] + grid[i][j+1] + grid[i+1][j])/4); different = fabs(grid[i][j] - initial); if(different > maxDifference){ maxDifference = different; } } } return maxDifference; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
