Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Book: Engineering Problem Solving with C++ third edition Question: pg. 430 #19 (

ID: 3570002 • Letter: B

Question

Book: Engineering Problem Solving with C++ third edition

Question: pg. 430 #19 (code must be C++ format)

*****ORIGINAL QUESTION FROM BOOK*****

Temperature Distribution. The temperature distribution in a thin metal plate with constant (or isothermal) temperatures on each side can be modeled using a two-dimensional grid. Typically, the number of points in the grid are specified, as are the constant temperatures on the four sides. The temperatures of the interior points are usually initialized to zero, but they change according to the temperatures around them. Assume that the temperature of an interior point can be computed as the average of the four adjacent temperatures. Each time that the temperature of an interior point changes, the temperatures of the points adjacent to it change. These changes continue until a thermal equilibrium is achieved and all temperatures become constant.

- Write a program to model this temperature distribution for a grid with six rows and eight columns. Allow the user to enter the temperatures for the four sides. Use one grid to store the temperatures. Thus, when a point is updated, its new value is used to update the next point. Continue updating the points, moving across the tolerance value. Use the vector class to implement the grid.

*****MODIFICATIONS TO QUESTION*****

-Use a 2-D array NO vectors.

-Prompt for the 4 temperature values on the four sides.

-Then prompt for the number of simulations. Rather than calculating the tolerance, we will just run the simulation a specific number of times. So you may want to calculate the temperature distribution over 20 simulations.

-Program will use functions

*****OUTPUT SHOULD INCLUDE:*****

-Echo of all input

-2-D array in a neat and readable format

*****HINTS:*****

-Use global constants for NROWS and NCOLS

-Should include a function to prompt for values

-A function to calculate the temperatures

-A function to output the results

-Main will be VERY simple

-All of the other functions will only be CALLED once

Explanation / Answer

Temperature Distribution

/*--------------------------------------------------------------------*/

/* Problem chapter8_19                                               */

/*                                                                    */

/* This program models a temperature distribution for a grid         */

/* with six rows and eight columns. The user enters the             */

/* temperatures for the four sides. When a point is updated, its    */

/* new value is used to update the next point. Updating continues,   */

/* moving across the rows, until the temperature differences of      */

/* all updates are less than a user-entered tolerance value.         */

#include <iostream>

#include <cstdlib>

#include <cmath>

#include <iomanip>

#include <vector>

using namespace std;

const int NROWS = 8;

const int NCOLS = 8;

int main()

{

   /* Declare and initialize variables. */

   int row, col;

   bool done(false);

   vector< vector<double> > t(NROWS);

   double top, right, left, bottom, tolerance,

          check, update, maxDiff=0;

   /* Prompt user to enter initial temperatures and tolerance. */

   cout << "Enter initial temperatures (top, right, bottom, left): ";

   cin >> top >> right >> bottom >> left;

   cout << "Enter tolerance for equilibrium: (>0) ";

   cin >> tolerance;

   tolerance = fabs(tolerance);

   t[0].resize(NCOLS); //allocate columns for first row

   /* Initialize grid. */

   for (row=1; row<NROWS-1; row++)

   {

      for (col=1; col<NCOLS-1; col++)

      {

         t[col].resize(NCOLS); //sllocate columns for middle rows

         t[row][col] = 0.0;

      }

      t[row][0] = left;

      t[row][NCOLS-1] = right;

   }

   t[NROWS-1].resize(NCOLS); //sllocate columns for last row

   for (col=0; col<NCOLS; col++)

   {

      t[0][col] = top;

      t[NROWS-1][col] = bottom;

   }

/* Update the grid across the rows. */

   while(!done)     //(maxDiff > tolerance) || (maxDiff < -tolerance));

   {

      /* Initialize the maximum update this iteration to zero */

      maxDiff = 0.0;

      /* Interior columns */

      for(row=1; row<NROWS-1; row++)

      {

         for (col=1; col<NCOLS-1; col++)

         {

            update = (t[row+1][col] + t[row-1][col] + t[row][col-1]

                     + t[row][col+1])/4;

            check = fabs(update - t[row][col]);

            if (check > maxDiff)

               maxDiff = check;

            t[row][col] = update;

         }

      }

      done = (maxDiff < tolerance);

   }

   /* Print results. */

   cout << "Equilibrium values:" << endl;

   for (row=0; row<NROWS; row++)

   {

      for (col=0; col<NCOLS; col++)

         cout << setw(10) << t[row][col];

      cout << endl;

   }

   /* Exit program. */

   return 0;

}

/*--------------------------------------------------------------------*/