Topical Information

This lab will help you practice with dynamic memory (NOT mixed with classes).

Program Information

Heat flow through a rod can be simulated fairly easily in a computer program. We can simulate the rod using an array of temperatures. The rod begins all at the same temperature (user determined), but holding some position(s) of the rod at constant temperature (holding a match or ice cube to the rod) provides a [set of] heat source(s) (or sink(s)).

To update the temperature at each time step (second?), you take the average of the positions on each side from the previous time step (and at the current position). For instance, if the following was the initial state of the rod:

+------+------+------+------+------+------+------+------+
|  10  |  10  |  10  |  10  |  10  |  10  |  10  |  10  |
+------+------+------+------+------+------+------+------+

And then the user specifies that there is a heat source at the left end of 100 degrees:

+------+------+------+------+------+------+------+------+
| 100  |  10  |  10  |  10  |  10  |  10  |  10  |  10  |
+------+------+------+------+------+------+------+------+

Then the next time step would result in:

+------+------+------+------+------+------+------+------+
| 100  |  40  |  10  |  10  |  10  |  10  |  10  |  10  |
+------+------+------+------+------+------+------+------+

Note how the left end stays the same as it is the heat source and won't change. The next position, however, becomes the average of itself and its two neighbors. Likewise the position after that becomes the average of itself and its two neighbors (it just happens that its two neighbors and it all had the same value — since they all started out the same).

A second time step would result in:

+------+------+------+------+------+------+------+------+
| 100  |  50  |  20  |  10  |  10  |  10  |  10  |  10  |
+------+------+------+------+------+------+------+------+

After many time steps have passed, the heat will flow through the rod and all 7 positions not attached to a heat source will eventually change.

Speaking of which, what do you do at the end of the rod? When you hit the end of the rod you don't have two neighbors, do you? You've only got the one previous neighbor! Just take the average of yourself and that one neighbor, then. Problem solved!


You should allow the user to enter the length of the rod, the number of time steps to simulate, the positions and temperatures of any heat sources (sinks) (notice I'm not specifying how many sources/sinks there are), and the number of steps to skip between printings (if they simulate for 2 minutes, they probably don't need to see all 120 time steps...maybe just every 20th one or so).

Always show the initial (heat sources applied) and final (after the last time step) states of the rod — no matter how many skipped time steps they specify!

Use as little memory as possible...

Thought Provoking Questions

  1. What types of values are the temperatures? (Hint: they are being averaged over 3 — or over 2 at the ends.)

  2. How do you dynamically allocate memory for the temperature array(s)?

  3. Do you need to do anything special after you attempt allocation?

  4. What about after you are done with the OS's heap memory?

  5. What's this value nullptr all about?

  6. How can you easily show the rod only on every nth iteration? (Hint: think "iteration divisible by n".)

  7. How will you know which position a heat source/sink is at? How many sources/sinks will you allow? Perhaps if you ask them how many they have it might help..?

  8. How do you average the temperatures for time n+1 from time n without upsetting the averages? (Note: in the above example, the value of 20 came as the average of 40+10+10 — not 50+10+10.) (Hint: maybe a second array?)

  9. Although your arrays are dynamic, do the functions which update, initialize, and print the rod's temperatures have to know that the arrays they are working with are dynamic?

  10. When you update which array is time n and which is time n+1, does the fact that the arrays are dynamic benefit you in any way?

This assignment is (Level 2).