%3Cp%3ETWO-DIMENSIONAL%20VECTORS%3C%2Fp%3E%0A%3Cp%3E%0AIn%26nbsp%3Bthis%26nbsp%3
ID: 3545876 • Letter: #
Question
%3Cp%3ETWO-DIMENSIONAL%20VECTORS%3C%2Fp%3E%0A%3Cp%3E%0AIn%26nbsp%3Bthis%26nbsp%3Blab%26nbsp%3Byou%26nbsp%3Bwill%26nbsp%3Bgain%26nbsp%3Bexperience%26nbsp%3Bwriting%26nbsp%3Band%26nbsp%3Busing%26nbsp%3Bfunctions%26nbsp%3Bwith%26nbsp%3Bvectors%26nbsp%3Bfrom%26nbsp%3Bthe%26nbsp%3BSTL.%26nbsp%3BHEAT%3C%2Fp%3E%0A%3Cp%3E%0AThis%26nbsp%3Bassignment%26nbsp%3Bis%26nbsp%3Bbased%26nbsp%3Bupon%26nbsp%3Bthe%26nbsp%3BTemperature%26nbsp%3BDistribution%26nbsp%3Bproblems%26nbsp%3B(19-22)%26nbsp%3Bin%26nbsp%3BChapter%26nbsp%3B8%26nbsp%3Bof%26nbsp%3Byour%26nbsp%3Btextbook%2C%26nbsp%3Bbut%26nbsp%3Bwith%26nbsp%3Bthe%26nbsp%3Bfollowing%26nbsp%3Bchanges%3A%3C%2Fp%3E%0A%3Cp%3E%0A1.%26nbsp%3BPut%26nbsp%3Byour%26nbsp%3Bmain()%26nbsp%3Bfunction%26nbsp%3Bin%26nbsp%3Ba%26nbsp%3Bseparate%26nbsp%3Bsource%26nbsp%3Bfile%26nbsp%3Bnamed%26nbsp%3BheatMain.cpp.%3C%2Fp%3E%0A%3Cp%3E%0A2.%26nbsp%3BCreate%26nbsp%3Bthree%26nbsp%3Bfunctions%26nbsp%3B%E2%80%93%26nbsp%3Bprint2Dvector()%2C%26nbsp%3Brelax()%2C%26nbsp%3Binitialize()%26nbsp%3Band%26nbsp%3Bplace%26nbsp%3Bthem%26nbsp%3Bin%26nbsp%3Ba%26nbsp%3Bsource%26nbsp%3Bfile%26nbsp%3Bnamed%26nbsp%3Bheat.cpp.%26nbsp%3BThese%26nbsp%3Bthree%26nbsp%3Bfunctions%26nbsp%3Bare%26nbsp%3Bdescribed%26nbsp%3Bbelow.%26nbsp%3BRELAX%26nbsp%3BFUNCTION%3C%2Fp%3E%0A%3Cp%3E%0AThe%26nbsp%3Bprototype%26nbsp%3Bfor%26nbsp%3Bthe%26nbsp%3Brelax()%26nbsp%3Bfunction%26nbsp%3Bis%3A%26nbsp%3Bdouble%26nbsp%3Brelax(vector%26lt%3Bvector%26lt%3Bdouble%26gt%3B%26nbsp%3B%26gt%3B%26nbsp%3B%26amp%3Bplate)%3C%2Fp%3E%0A%3Cp%3E%0AThe%26nbsp%3Brelax()%26nbsp%3Bfunction%26nbsp%3Bchanges%26nbsp%3Bplate%26nbsp%3Bin%26nbsp%3Bthe%26nbsp%3Bcaller%26nbsp%3Bby%26nbsp%3Bchanging%26nbsp%3Bthe%26nbsp%3Btemperature%26nbsp%3Bof%26nbsp%3Beach%26nbsp%3Bcell%26nbsp%3Bin%26nbsp%3Bthe%26nbsp%3Bgrid%26nbsp%3Bto%26nbsp%3Bbe%26nbsp%3Bthe%26nbsp%3Baverage%26nbsp%3Bof%26nbsp%3Bthe%26nbsp%3Btemperatures%26nbsp%3Bin%26nbsp%3Bthe%26nbsp%3Bfour%26nbsp%3Bneighboring%26nbsp%3Bcells.%26nbsp%3BIt%26nbsp%3Bperforms%26nbsp%3Bonly%26nbsp%3Bone%26nbsp%3Biteration%2C%26nbsp%3Bwhich%26nbsp%3Bconsists%26nbsp%3Bof%26nbsp%3Bvisiting%26nbsp%3Beach%26nbsp%3Bnon-border%26nbsp%3Bcell%26nbsp%3Band%26nbsp%3Bfinding%26nbsp%3Bits%26nbsp%3Bnew%26nbsp%3Btemperature%2C%26nbsp%3Bbased%26nbsp%3Bon%26nbsp%3Bthe%26nbsp%3Btemperatures%26nbsp%3Bin%26nbsp%3Bthe%26nbsp%3Bfour%26nbsp%3Bneighboring%26nbsp%3Bcells.%26nbsp%3BIt%26nbsp%3Breturns%26nbsp%3Bthe%26nbsp%3Bmaximum%26nbsp%3Babsolute%26nbsp%3Bchange%26nbsp%3B(always%26nbsp%3Bpositive)%26nbsp%3Bfound%26nbsp%3Bduring%26nbsp%3Bthat%26nbsp%3Biteration.%26nbsp%3BThe%26nbsp%3Bneighboring%26nbsp%3Bcells%26nbsp%3Bare%26nbsp%3Bthe%26nbsp%3Bcells%26nbsp%3Babove%2C%26nbsp%3Bbelow%2C%26nbsp%3Bto%26nbsp%3Bthe%26nbsp%3Bleft%2C%26nbsp%3Band%26nbsp%3Bto%26nbsp%3Bthe%26nbsp%3Bright%26nbsp%3Bof%26nbsp%3Bthe%26nbsp%3Bcell%26nbsp%3Bin%26nbsp%3Bquestion.%26nbsp%3BThe%26nbsp%3Btemperatures%26nbsp%3Bin%26nbsp%3Beach%26nbsp%3Biteration%26nbsp%3Bdepend%26nbsp%3Bonly%26nbsp%3Bon%26nbsp%3Bthe%26nbsp%3Btemperature%26nbsp%3Bfrom%26nbsp%3Bthe%26nbsp%3Bprevious%26nbsp%3Biteration%26nbsp%3Band%26nbsp%3Bnot%26nbsp%3Bon%26nbsp%3Bany%26nbsp%3Bchanges%26nbsp%3Bwrought%26nbsp%3Bduring%26nbsp%3Bthe%26nbsp%3Bcurrent%26nbsp%3Biteration.%26nbsp%3BThus%2C%26nbsp%3Bthe%26nbsp%3Btemperatures%26nbsp%3BTi(row%2C%26nbsp%3Bcol)%26nbsp%3Bat%26nbsp%3Bthe%26nbsp%3Bend%26nbsp%3Bof%26nbsp%3Biteration%26nbsp%3Bi%26nbsp%3Bare%3A%3C%2Fp%3E%0A%3Cp%3E%0ATi(row%2C%26nbsp%3Bcol)%26nbsp%3B%3D%26nbsp%3B(Ti-1(row%2B1%2C%26nbsp%3Bcol)%26nbsp%3B%2B%26nbsp%3BTi-1(row-1%2C%26nbsp%3Bcol)%26nbsp%3B%2B%26nbsp%3BTi-1(row%2C%26nbsp%3Bcol%2B1)%26nbsp%3B%2B%26nbsp%3BTi-1(row%2C%26nbsp%3Bcol-1)%26nbsp%3B)%2F4%3B%3C%2Fp%3E%0A%3Cp%3E%0Awhere%26nbsp%3BTi-1(row%2C%26nbsp%3Bcol)%26nbsp%3Bis%26nbsp%3Bthe%26nbsp%3Btemperature%26nbsp%3Bat%26nbsp%3Bthe%26nbsp%3Brow%26nbsp%3Band%26nbsp%3Bcolumn%26nbsp%3Bat%26nbsp%3Bthe%26nbsp%3Bend%26nbsp%3Bof%26nbsp%3Bthe%26nbsp%3Bprevious%26nbsp%3Biteration.%3C%2Fp%3E%0A%3Cp%3EINITIALIZE%26nbsp%3BFUNCTION%3C%2Fp%3E%0A%3Cp%3E%0AThe%26nbsp%3Bprototype%26nbsp%3Bfor%26nbsp%3Bthe%26nbsp%3Binitialize()%26nbsp%3Bfunction%26nbsp%3Bis%3A%26nbsp%3Bvoid%26nbsp%3Binitialize(vector%26lt%3Bvector%26lt%3Bdouble%26gt%3B%26nbsp%3B%26gt%3B%26nbsp%3B%26amp%3Bplate%2C%26nbsp%3Bdouble%26nbsp%3BtopTemp%2C%26nbsp%3Bdouble%26nbsp%3BbottomTemp%2C%26nbsp%3Bdouble%26nbsp%3BleftTemp%2C%26nbsp%3Bdouble%26nbsp%3BrightTemp)%3C%2Fp%3E%0A%3Cp%3E%0Ainitialize()%26nbsp%3Bsets%26nbsp%3Bup%26nbsp%3Bthe%26nbsp%3Bborders%26nbsp%3Bcells%26nbsp%3Bwith%26nbsp%3Bthe%26nbsp%3Bspecified%26nbsp%3Btemperatures.%26nbsp%3BAs%26nbsp%3Ban%26nbsp%3Bexample%2C%26nbsp%3Bcalling%26nbsp%3Binitialize()%26nbsp%3Bwith%26nbsp%3Binitialize(plate%2C%26nbsp%3BtopTemp%2C%26nbsp%3BbottomTemp%2C%26nbsp%3BleftTemp%2C%26nbsp%3BrightTemp)%3B%3C%2Fp%3E%0A%3Cp%3E%0Awhere%26nbsp%3BtopTemp%26nbsp%3B%3D%26nbsp%3B32%2C%26nbsp%3BbottomTemp%26nbsp%3B%3D%26nbsp%3B64%2C%26nbsp%3BleftTemp%26nbsp%3B%3D%26nbsp%3B100%2C%26nbsp%3Band%26nbsp%3BrightTemp%26nbsp%3B%3D%26nbsp%3B80%3C%2Fp%3E%0A%3Cp%3E%0Ashould%26nbsp%3Bresult%26nbsp%3Bin%26nbsp%3Bthe%26nbsp%3B2D%26nbsp%3Bvector%2C%26nbsp%3Bplate%2C%26nbsp%3Bcontaining%3A%3C%2Fp%3E%0A%3Cp%3EPRINT2DVECTOR%26nbsp%3BFUNCTION%3C%2Fp%3E%0A%3Cp%3E%0AThe%26nbsp%3Bprototype%26nbsp%3Bfor%26nbsp%3Bthe%26nbsp%3Bprint2Dvector()%26nbsp%3Bfunction%26nbsp%3Bis%3A%26nbsp%3Bvoid%26nbsp%3Bprint2Dvector(vector%26lt%3Bvector%26lt%3Bdouble%26gt%3B%26nbsp%3B%26gt%3B%26nbsp%3Bdata)%3C%2Fp%3E%0A%3Cp%3E%0AThe%26nbsp%3Bfunction%26nbsp%3Bprint2Dvector()%26nbsp%3Bsimply%26nbsp%3Bprints%26nbsp%3Bout%26nbsp%3Beach%26nbsp%3Brow%26nbsp%3Bof%26nbsp%3Bthe%26nbsp%3B2D%26nbsp%3Bvector%26nbsp%3Bon%26nbsp%3Ba%26nbsp%3Bseparate%26nbsp%3Bline%2C%3C%2Fp%3E%0AExplanation / Answer
#include <vector>
#include <math.h>
#include <iostream>
using namespace std;
double relax(vector<vector<double> > &plate) {
int cols = plate[0].size();
vector<double> temp(cols, 0);
vector< vector<double> > newplate(plate.size(), temp);
double maxchange = 0;
double change = 0;
for (int i=0; i < plate.size(); i++) {
for(int j=0; i < plate[0].size(); j++) {
if (i == 0 || j == 0 || i == plate.size() - 1 || j == plate[0].size() - 1) { // border cells donot change
newplate[i][j] = plate[i][i];
}
else {
newplate[i][j] = (plate[i+1][j] + plate[i-1][j] + plate[i][j+1] + plate[i][j-1])/4;
change = fabs(newplate[i][j] - plate[i][j]);
if (change > maxchange) maxchange = change;
}
}
}
for (int i=0; i < plate.size(); i++) {
for(int j=0; i < plate[0].size(); j++) {
plate[i][j] = newplate[i][j];
}
}
return maxchange;
}
void initialize(vector<vector<double> > &plate, double topTemp, double bottomTemp, double leftTemp, double rightTemp) {
int rows = plate.size();
int cols = plate[0].size();
for(int i = 0; i < cols; i++) {
plate[0][i] = topTemp;
plate[rows-1][i] = bottomTemp;
}
for(int i = 0; i < rows; i++) {
plate[i][0] = leftTemp;
plate[i][cols-1] = rightTemp;
}
}
void print2Dvector(vector<vector<double> > plate) {
cout << "Printing heat vector:: " << endl;
for (int i=0; i < plate.size(); i++) {
for(int j=0; i < plate[0].size(); j++) {
std::cout << plate[i][j] << " ";
}
std::cout << endl;
}
}
int main() {
vector<vector<double> > plate(10, vector<double>(10,0));
initialize(plate, 32, 64, 100, 80);
double change = relax(plate);
std::cout << "Maxchange : " << change << std::endl;
print2Dvector(plate);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.