Topical Information Use C++. 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...
Topical Information
Use C++. 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...
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images