Requirements Part 1 - Initialize and Print 2D Array (4 points) The problem is to
ID: 3758081 • Letter: R
Question
Requirements
Part 1 - Initialize and Print 2D Array (4 points)
The problem is to determine the temperature distribution for a two-dimensional plate with constant boundary conditions. Use a 20 X 20 two-dimensional array of numbers to represent the temperature at different places on the (square) plate. The elements on the boundaries have fixed temperatures. The elements on the top and bottom row have a fixed temperature of 100 degrees. The elements on the leftmost and rightmost columns have a temperature of zero degrees. (The corner elements have a temperature of zero degrees.)
NOTE: You should NOT have commas when you are printing the array. You should only have commas in your export function.
You must print the initialized 2D array to the screen.
For example, an initialized 4 X 4 matrix (note: yours should be 20x20) would look like this. You must initialize all squares not on the edge to 0:
0
100
100
0
0
0
0
0
0
0
0
0
0
100
100
0
Part 2 - Update Elements Once (4 points)
The job of the algorithm is to find the steady-state temperature distribution of the interior elements which are constantly changing to become the average of the temperature of their neighbors. Steady state means that the temperature of any cell is virtually unchanging and approximately equal to the average of the temperatures of its four neighbors on the north, south, east, and west directions. Note that the steady state temperatures will be the same regardless of what the beginning temperatures are for the interior elements.
To calculate the new temperature value of an array element, take the average of the values in the 4 neighbors of that cell from the previous iteration. This value will be placed in the output array.
For example, the cell at position array[1][2] currently has a value of 60.6 degrees. At the current iteration, the element in the output array will be set to the average of its 4 CURRENT neighbors (100.0 + 66.3 + 34.0 + 45.2)/4.
Print the first iteration.
NOTE: MAKE SURE TO WORK ON THE TWO 2D ARRAYS, ONE OF WHICH HAS THE TEMPERATURES FROM THE LAST ITERATION AND THE OTHER CONTAINING THE NEW, AVERAGED TEMPERATURES.
FOR THE NEXT ITERATION, THE NEW, AVERAGED TEMPERATURES BECOME THE INPUT TEMPERATURES.
Part 3 - Repeat update until Stable (8 points)
You should continue to iterate until no cell in the array changes more than 0.1 degree, calculating the temperature for all interior cells on each iteration. Your program should monitor the largest change for any cell in the array in order to determine when to stop iterating (See Common Loop Algorithms in section 4.7 of the book for ideas).
Part 4 - Using Excel to Display Results (4 points)
When you are finished, you should write your two-dimensional array out to a comma separated values file like this:
Essentially, all you need to do is write to a file that's named with a .csv extention (Please name your file "lab6output.csv"). Look to the book for help in writing to a file and using ostream objects.
You can import this csv file into a spreadsheet like excel and format it as a 3D graph in order to check your work. If you've done your calculations correctly, your graph will look like this.
Good Coding Style (5 points)
Does your code exemplify the good coding habits taught in class?
Requirement Notes
Make sure you use functions and two 2D arrays (not vectors).
To graph your data, import the csv file into excel as a csv file, then select the data and create a 3D chart. To change the rotation of the chart, right click on the image and the click on 3D rotation. Change the values of X and Y. Follow this tutorial if you're unfamiliar with Excel: Export To Excel
XCODE USERS: In order for you to write the .csv file, you will need to change your working directory. To change your working directory, go to Product->Scheme->Edit Scheme. In the window that pops up, click "Debug" from the list on the left side. Check the "Custom Working Directory" box, and select the location of your main file. After that, it should work the same way as Visual Studio.
You must not use Global Variables (Global Constants are allowed)
Requirement For Pass Off
You must implement at least the following functions
Initialize Hot Plate
Average Hot Plate (one time through the 2D array)
Print Hot Plate
Export Hot Plate
No Test Cases are Required for this Lab.
No Magic Numbers (rows, columns, .1 , etc)
FAQ
Below is a list of frequently-asked-questions:
Make sure that your program is *generating* the CSV file. Don't just include a CSV file in the zip file you submit: it will be ignored. Also, make sure you create your CSV file in the current directory, e.g. "lab6output.csv" instead of "/Users/farrell/Desktop/lab6output.csv".
When using setprecision( ), you'll likely want to use the fixed keyword first. Fixed makes it use fixed floating-point notation (e.g. 100.00) as opposed to scientific floating-point notation (e.g. 1e2).
Do not use the <array> library. You do not need it to complete the lab. If you try to use it, your submission may not compile.
Do not name your function "export". While Visual Studio doesn't complain, this is a reserved keyword to most C++ compilers (including the one used by the autograder).
Hint: in your steady-state array (the one dumped to the CSV file), the value in the 1,1 entry (second row, second column) should be 49.8.
0
100
100
0
0
0
0
0
0
0
0
0
0
100
100
0
Explanation / Answer
CODE :
#include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; const int SIZE = 20; const double CHANGE = 0.1; void initial(double hot_plate[SIZE][SIZE]) { for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { if (row == 0 || row == 19) { if (col == 0 || col == 19) { hot_plate[row][col] = 0; } else { hot_plate[row][col] = 100; } } else { hot_plate[row][col] = 0; } } } } void print(double hot_plate[SIZE][SIZE]) { for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { cout << fixed << setprecision(1) << setw(6) << hot_plate[row][col] << ","; } cout << endl; } } void first_average(double hot_plate[SIZE][SIZE]) { const double NEIGHBORS = 4.0; double average_temp; for (int row = 1; row < SIZE - 1; row++) { for (int col = 1; col < SIZE - 1; col++) { average_temp = (hot_plate[row][col - 1] + hot_plate[row + 1][col] + hot_plate[row][col + 1] + hot_plate[row - 1][col]) / NEIGHBORS; hot_plate[row][col] = average_temp; } } } void average(double hot_plate[SIZE][SIZE]) { const double NEIGHBORS = 4.0; double average_temp; double largest_change = 0; do { largest_change = 0; for (int row = 1; row < SIZE - 1; row++) { for (int col = 1; col < SIZE - 1; col++) { double old_value = hot_plate[row][col]; average_temp = (hot_plate[row][col - 1] + hot_plate[row + 1][col] + hot_plate[row][col + 1] + hot_plate[row - 1][col]) / NEIGHBORS; hot_plate[row][col] = average_temp; double change = abs(old_value - hot_plate[row][col]); if (change > largest_change) { largest_change = change; } } } } while (largest_change > CHANGE); } void export(double hot_plate[SIZE][SIZE]) { ofstream out; out.open("hot_plate.csv"); for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { out << hot_plate[row][col] << ","; } out << endl; } } int main() { double hot_plate[SIZE][SIZE]; initial(hot_plate); print(hot_plate); cout << endl << endl; first_average(hot_plate); print(hot_plate); cout << endl << endl; average(hot_plate); print(hot_plate); export(hot_plate); system("pause"); return 0; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.